【发布时间】:2023-04-02 05:25:01
【问题描述】:
我能够成功运行 curl,但无法使用 python 请求 mailchimp API。它给出了错误like urllib2.HTTPError: HTTP Error 401: Unauthorized
卷曲
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 请求:
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)
我已经推荐了Converting cURL to Python Requests,但没有成功。
【问题讨论】:
-
我试过两个机器人都不起作用。这是带有 urllib2 的代码。
-
@VigneshPrajapati 我遇到了同样的问题。您找到解决方案或解决方法了吗?
-
嗨@giliev,我已经解决了。这是数据中心的 URL 问题。我使用的是 us9.api... 而不是 us6.api.mailchimp.com/3.0/lists,因为我的帐户是在 us6 下创建的。因此,您需要将数据中心与请求中的 URL 相匹配。
标签:
python
authentication
curl
mailchimp
mailchimp-api-v3.0
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:将 mailchimp curl 请求转换为 python 请求 - Python技术站