我正在尝试使用 Python 和 Constant Contact API 将新联系人推送到 Constant Contact 并将他们添加到列表中。到目前为止,我已经尝试了以下方法:
import requests
url = 'https://api.cc.email/v3/contacts'
url_completed = url + '?api_key=' + api_key
post_request = {
"email_address": {
"address": "abc123@gmail.com"
},
"first_name": "First",
"last_name": "Last",
"create_source": "Account",
"list_memberships": ["xxxxxxxxx"]
}
headers = {
'Authorization': 'Bearer xxxxxxx',
'content_type': 'application/json'
}
resp = requests.post(url_completed, data=post_request, headers=headers)
print(resp.raise_for_status())
但是当我运行它时,我得到一个错误 401 Client Error: Unauthorized for url: https://api.cc.email/v3/contacts?api_key=xxxxxxx
为什么会出现此错误?为了确保我正确地做到了这一点,为了了解更多背景信息,我进入了持续联系dev portal。并创建了一个新的应用程序。它给了我一个 API key ,我将其插入到 URL 中,还有一个 secret ,我认为它是不记名 token ,因此将其替换在那里。我还使用 Constant Contact 引用文档 UI 测试了负载,它允许在 OAuth 身份验证后测试负载,并且成功了。具体来说,我正在使用 this endpoint .
最佳答案
401 表示“访问 token 无效”。最有可能的是,您不能使用应用程序提供的 API key ,而必须使用 OAuth 2.0 流程提供的专用访问 token 。阅读更多关于 OAuth 授权的信息 here .
https://stackoverflow.com/questions/68186574/
相关文章:
javascript - Jest "Cannot log after tests are don
amazon-web-services - Terraform 在配置 S3 后端时出错
java - 在没有调试器的情况下在 VS Code 中导航 Java 堆栈跟踪
react-native - 升级到 npm7 但库中断太多
javascript - 如何在 vscode 中查看全 Angular 代码
java - Spring boot 从附加位置文件覆盖 prod 属性
Angular - 访问 MatMenu 的 elementRef