简体   繁体   中英

Convert mailchimp curl request to python request

I am able to run curl successfully but not worked with python request for mailchimp API.It gives an error like urllib2.HTTPError: HTTP Error 401: Unauthorized

CURL

curl --request POST \
--url 'https://us9.api.mailchimp.com/3.0/lists' \
--user 'anystring:6a983664930fc8ba1eecdsdf334344f40-us9' \
--header 'content-type: application/json' \
--data '{"name":"My test","contact":{"company":"Cool","address1":"Awesome place","city":"Lanka","state":"MH","zip":"43472","country":"IN","phone":""},"permission_reminder":"You'\''re receiving this email because you signed up.","campaign_defaults":{"from_name":"VD","from_email":"hey@sdfsdf.com","subject":"","language":"en"},"email_type_option":true}' \
--include

Python request :

import urllib2
import json
import requests
url = 'https://us9.api.mailchimp.com/3.0/lists/'

all_params={"user":"my_username:6a983664930fc8ba1eecd1d5d68f4f40-us9",
           "name":"My test",
           "contact":{"company":"Cool","address1":"Awesome place","city":"Lanka","state":"MH","zip":"43472","country":"IN","phone":""},"permission_reminder":"You'\''re receiving this email because you signed up.","campaign_defaults":{"from_name":"VD","from_email":"hey@sdfsdf.com","subject":"","language":"en"},
           "email_type_option":'true'}

post_data = urllib2.quote(json.dumps(all_params))
headers = {'Content-Type': 'application/json'}
request = urllib2.Request(url, post_data, headers) 
response = urllib2.urlopen(request)

I have referred Converting cURL to Python Requests but not worked.

I had solved it. It was problem of URL with data center. I was using us9.api... instead of us6.api.mailchimp.com/3.0/lists because my account was created under us6. So, I need to use data center where my account is registered with my URL in request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM