2

I am trying to send a POST request to the mailchimp api to add a new member to a list, but I keep getting an error saying that the api key is missing. When I send a get request I include the key in the URL and it works normal.

From the Mailchimp documentation it looks as though the api key should be part of the request and the parameters (email_address and status) part of the body, but I don't understand how to do that using requests. (I know there is a mailchimp module for Python, but I have some custom things to do and I want to get my head around this)

This is the POST I am trying to get going:

import requests
url="https://us15.api.mailchimp.com/3.0/lists/xxxxx/members/"

header ={"Authorization":"apikey xxxxx",
"email_address":"test@gmail.com",
"status":"subscribed"}

r=requests.post(url,header)
print r.text

The error I get reads: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"API Key Missing","status":401,"detail":"Your request did not include an API key.","instance":""}

I also tried to put this request in Postman where you can easily separate the header and the body, but it gives the same response.

I also tried it using the Mailchimp3 package, but that gives me a bad request. The code I used is as follows:

from mailchimp3 import MailChimp

client = MailChimp('edgar@mymailserver.nl', '{}-{}'.format('xxxxxxxxxxxxxxxxxxxxxxx','us15'))

client.lists.members.create('2be23de2cc', {'email_address': 'john.doe@example.com','status': 'subscribed'})

However, I get the following error: requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://us15.api.mailchimp.com/3.0/lists/2be23de2cc/members

edgar piet
  • 67
  • 3
  • 11
  • have you tried `curl --request POST \ --url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' \ --user 'anystring:apikey' \ --header 'content-type: application/json' \ --data '{"email_address":"urist.mcvankab+3@freddiesjokes.com", "status":"subscribed"}' \ --include` reference https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#Add or update a list member try sending api key like --user 'anystring:apikey' – Aditya May 17 '17 at 10:02
  • 1
    I saw the curl, but I don't understand how to put such a request in python code, so I did not know how to try. – edgar piet May 17 '17 at 10:57
  • If you are not sure of how to send request in python use this [package](https://pypi.python.org/pypi/requests/2.7.0) – Aditya May 18 '17 at 06:12
  • Isn't that exactly the package I use in the code I posted in the question or am I not getting it? – edgar piet May 22 '17 at 13:03

2 Answers2

6

Why don't you give this package a try? https://github.com/charlesthk/python-mailchimp

You can do that by:

from mailchimp3 import MailChimp
client = MailChimp('my_user', '{}-{}'.format(access_token, data_center))
client.lists.members.create('my_list_id', {'email_address': 'test@gmail.com', 'status': 'subscribed'})

When testing it, please do not test with fake emails because Mailchimp has a kind of global ban list to ignore the emails used by spammers. In this case, it will return a HTTP 400.

Vinicius Ronconi
  • 181
  • 1
  • 10
  • I tried the package too but no matter what I try, I get a bad request that looks like this: requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://us15.api.mailchimp.com/3.0/lists/2be23de2cc/members. And since I can not see the actual request through the package, I have no idea how to continue. – edgar piet May 22 '17 at 13:00
  • Can you provide the code you used? And try to test it on the [Mailchimp Playground](https://api.mailchimp.com/playground/), maybe it will help you to realize what is going wrong. – Vinicius Ronconi May 22 '17 at 19:12
  • I put the code I used in the original question, including the error I got. I will do some more mining in the playground as well, but for as far as I can tell, I do exactly what you write. And the API seems to work find for GET requests. – edgar piet May 23 '17 at 08:44
  • In the playground I get a slightly different error though. In there it reads: "message: Schema describes object, NULL found instead." Does that mean I am not formatting the object correctly? – edgar piet May 23 '17 at 08:54
  • 1
    Are you testing only with fake emails or with real ones? Mailchimp has a kind of "ban list" and that could be the reason. – Vinicius Ronconi May 23 '17 at 10:40
4

Ignoring the typo in your code header ={"Authorization","apikey xxxxx",, you don't send credentials or the data in the header. You first need to authenticate, then send the subscriber information as a POST payload, like this:

import pprint
import requests

username = 'foo'
apikey = 'sekret-key-goes-here'
url = "https://us15.api.mailchimp.com/3.0/lists/xxxxx/members/"

post_params = {'email_address': 'foo@example.com', 'status': 'subscribed'}

r = requests.post(url, auth=(username, apikey), json=post_params)
r.raise_for_status()

results = r.json()
pprint.pprint(results)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    I tried your code, but it gives me an error too:requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://us15.api.mailchimp.com/3.0/lists/2be23de2cc/members/ I must be making a real silly mistake, but I can't get it figured out. – edgar piet May 23 '17 at 09:37
  • Do you mean the updated version of my own question? I did and it tells me that the API key is missing – edgar piet May 23 '17 at 11:08