我試圖使用Application-only authorization訪問Twitter API。我可以正確地得到一個無記號令牌(至少我認爲是這樣!),但是當我嘗試使用令牌進行API調用時,我總是得到Invalid or expired token
錯誤。嘗試訪問Twitter API時出現「無效或過期的令牌」錯誤
twitter_consumer_key = b"<MY_APP_CONSUMER_KEY>"
twitter_consumer_secret = b"<MY_APP_SECRET_KEY>"
twitter_keys = base64.standard_b64encode(twitter_consumer_key + b':' + twitter_consumer_secret)
content = {'grant_type': 'client_credentials'}
post_headers = {
b'Authorization': b'Basic ' + twitter_keys,
b'Content-Type': b'application/x-www-form-urlencoded;charset=UTF-8'
}
twitter_bearer = requests.post('https://api.twitter.com/oauth2/token', headers=post_headers, data=content).json()['access_token']
twitter_headers = {
b'Authorization': b'Bearer ' + base64.b64encode(twitter_bearer.encode('utf-8')),
}
response = requests.get("https://api.twitter.com/1.1/users/show.json?screen_name=itechbrij",
headers=twitter_headers).text
print(twitter_followers)
給{"errors":[{"code":89,"message":"Invalid or expired token."}]}
任何幫助表示讚賞。感謝