尝试获取推文时在 Twython 中收到 401 错误

问题描述

我正在尝试通过我的开发帐户获取来自某些 Twitter 用户的最新 100 条推文。我已验证凭据有效,因为我可以将 API 用于其他任务。但是,下面的代码会产生后续错误

from twython import Twython

with open("twitter_credentials.json","r") as file:
    creds = json.load(file)
    
python_tweets = Twython(creds['CONSUMER_KEY'],creds['CONSUMER_SECRET']) #object that is our credentials

query1 = {'q' : 'from:@Mike_Pence','count': 100,'exclude' : 'retweets'}

pence = python_tweets.get_user_timeline(**query1,tweet_mode='extended') #extended gives full text of tweets

for tweets in pence['statuses']:
    print(tweets['full_text'] + '\n')

我收到的错误是:

TwythonAuthError: Twitter API returned a 401 (Unauthorized),An error occurred processing your request.

如有任何帮助,我将不胜感激。

解决方法

您似乎过于频繁地使用 Twitter API。出于安全原因,Twitter 对任意应用程序中 Twitter API 的使用进行了速率限制。 Twitter 在 their post 中说明了他们的 Twitter API 速率限制是如何工作的。您最多可以在 15 分钟内从该特定应用的一组特定授权密钥发出 15 次 GET 请求。 15 分钟后,您可以自由设置接下来的 15 个请求。

您也可以尝试重新创建您的身份验证密钥