2016-05-12 184 views
1

我能够运行袅袅与蟒蛇请求成功,但没有工作了mailchimp API.It给出了一个错误like urllib2.HTTPError: HTTP Error 401: Unauthorized转换mailchimp卷曲请求

卷曲

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":"[email protected]","subject":"","language":"en"},"email_type_option":true}' \ 
--include 

的Python要求:

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":"[email protected]","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) 

我已经提到Converting cURL to Python Requests但没有工作。

+0

您是否正在尝试使用['urllib2'](https://docs.python.org/2.7/library/urllib2.html)或['requests'](http://www.python-requests.org)/EN /主/)?你的头衔说请求,并且你导入请求,但是你的代码不使用它,而是使用urllib2。 – jwodder

+0

我试过这两个机器人都没有工作。这是urllib2的代码。 –

+0

@VigneshPrajapati我有同样的问题。您是否找到解决方案或解决方法? – giliev

回答

0

我已经解决了它。是数据中心的URL的问题。我使用us9.api ...而不是us6.api.mailchimp.com/3.0/lists,因为我的帐户是在us6下创建的。所以,我需要使用数据中心,我的帐户在请求中通过我的网址注册。