2016-05-24 73 views
1

我試圖使用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."}]}

任何幫助表示讚賞。感謝

回答

0

嘗試這些變化BOLD

twitter_keys = base64.standard_b64encode((twitter_consumer_key + ':' + twitter_consumer_secret).encode( 'UTF8')

post_headers = { 'User-Agent':'您的用戶代理字符串', 'Authorization':'Basic'+ twitter_keys **。decode('utf8')**, 'Content-Type':'application/x-www -form-urlencoded; charset = UTF-8' }

twitter_bearer = requests.post( 'https://api.twitter.com/oauth2/token', 頭= post_headers, PARAMS =內容)上傳.json()[ 'ACCESS_TOKEN']

相關問題