【发布时间】:2018-12-26 12:54:43
【问题描述】:
我正在尝试从 twitter API 检索不记名令牌以允许仅应用程序身份验证,如 Twitter's API documentation 中所述。 conf.ini 包含 consumer_key 和 consumer_secret。
到目前为止,这是我的代码:
import configparser
import requests
import base64
conf = configparser.ConfigParser()
conf.read('conf/conf.ini')
consumer_key = conf['consumer-api-key']['value']
consumer_secret = conf['consumer-secret']['value']
key_secret = base64.urlsafe_b64encode('{}:{}'.format(consumer_key, consumer_secret).encode('ascii')).decode('ascii')
base_url = 'https://api.twitter.com/'
auth_url = '{}oauth2/token'.format(base_url)
auth_headers = {
'Authorization': 'Basic {}'.format(key_secret),
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
auth_data = {
'grant_type': 'client_credentials'
}
auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data)
print(auth_resp.status_code)
因此,我尝试了多种不同的方法,甚至重新生成了我的消费者密钥和秘密。在每种情况下,我都收到了以下回复:
'< HTTP/1.1 403 Forbidden' and {"errors":[{"code":99,"message":"Unable to verify your credentials","label":"authenticity_token_error"}.
我在故障排除中尝试了以下所有方法:
- 在 cURL 中手动写出命令。参考: curl -u $CONSUMER_KEY:$CONSUMER_SECRET" --compressed --data 'grant_type=client_credentials' 'https://api.twitter.com/oauth2/token'
- 删除 .ini,而只是将使用者密钥和秘密作为字符串导入
- 将Twython 与确切的示例源代码一起使用(使用我的密钥)。
- 等待 15 分钟,限速器超时。
- Logged out of Twitter,以防万一!
- 引用了推理in this blog post.
我也有人尝试将我的密钥与他们的代码一起使用,它能够成功地通过应用程序身份验证。我还检查了代码的每一步,我可以看到变量似乎都符合我的预期。
我知道这是一个重复的话题,但之前提供的答案似乎都没有解决它。 提前感谢您提供的任何帮助。
我继续在下面进行故障排除:
再次尝试使用 cURL 只是为了确保我的密钥有效:
curl -i -X POST -H "Authorization: Basic TOKEN" -d "grant_type=client_credentials" -H "Content-Type:application/x-www-form-urlencoded;charset=UTF-8" "https://api.twitter.com/oauth2/token"
HTTP/1.1 403 Forbidden
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-disposition: attachment; filename=json.json
content-length: 105
content-type: application/json;charset=utf-8
date: Wed, 18 Jul 2018 18:06:00 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Wed, 18 Jul 2018 18:06:00 GMT
ml: S
pragma: no-cache
server: tsa_b
status: 403 Forbidden
strict-transport-security: max-age=631138519
x-connection-hash: 4c3591363bea2e655f52dc9ec52aa5ee
x-content-type-options: nosniff
x-frame-options: DENY
x-response-time: 66
x-transaction: 001f3cd100c15114
x-tsa-request-body-time: 0
x-twitter-response-tags: BouncerCompliant
x-ua-compatible: IE=edge,chrome=1
x-xss-protection: 1; mode=block; report=https://twitter.com/i/xss_report
Connection: Keep-Alive
Set-Cookie: personalization_id="v1_Ro1inxBH+QRZGVyrYilreg=="; Expires=Fri, 17 Jul 2020 18:06:00 GMT; Path=/; Domain=.twitter.com
Set-Cookie: guest_id=v1%3A153193716084353644; Expires=Fri, 17 Jul 2020 18:06:00 GMT; Path=/; Domain=.twitter.com
{"errors":[{"code":99,"message":"Unable to verify your credentials","label":"authenticity_token_error"}]}
TOKEN 是...的结果。
base64.urlsafe_b64encode('{}:{}'.format(twconsumer_key, twconsumer_secret).encode('UTF-8')).decode('UTF-8')
【问题讨论】:
标签: python-3.x macos curl twitter twitter-oauth