Twitter API返回401(未授权),无效或过期令牌

问题描述 投票:5回答:1

我使用以下代码发布推文。这是一个非常标准的身份验证过程,但由于某种原因我无法进行身份验证。我得到的错误是Twitter API returned a 401 (Unauthorized), Invalid or expired token.

from twython import Twython, TwythonError
import requests

APP_KEY = 'rpOzpgp2FZNJqsq0' #fake key
APP_SECRET = 'FKBJWXOJwXTblhi1xBl4PtKgPemNFvumH' #fake secret

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

oauth_verifier_url = auth['auth_url']
oauth_verifier = requests.get(oauth_verifier_url)

# Getting the FINAL authentication tokens
final_step = twitter.get_authorized_tokens(oauth_verifier)

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

twitter.update_status(status='Yo')

这是完整的错误消息:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    final_step = twitter.get_authorized_tokens(oauth_verifier)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/twython/api.py", line 380, in get_authorized_tokens
    ken'), error_code=response.status_code)
twython.exceptions.TwythonError: Twitter API returned a 401 (Unauthorized), Invalid / expired To             ken

为什么我会收到此错误,我该怎么做才能修复它?

我尝试过多次重新生成我的密钥。我甚至删除了我的应用程序并多次创建了新的应用程序,但我仍然遇到此错误。

在“应用程序设置”下,我的访问级别设置为读取和写入:

Screenshot of app page

我不在防火墙后面。

我已经阅读了有关此站点上的Twython身份验证的其他解决方案,但他们似乎都提供了上述代码作为解决方案,但这本身并不适合我。

python python-3.x authentication twitter twython
1个回答
0
投票

我遇到了同样的问题,但在重新生成密钥后,问题就解决了。

© www.soinside.com 2019 - 2024. All rights reserved.