我正试图在Twitter上发帖,但我一直收到tweepy.errors.Unauthorized: 401 Unauthorized
。所以我决定使用api.verify_credentials()
来检查我是否能够连接,瞧:Authentication Successful
。但即便如此,这篇帖子仍然没有得到授权。
如何授权Tweepy发布到Twitter?
import tweepy
def main():
consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_token_secret = '...'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Authentication Successful")
except:
print("Authentication Error")
status = "Another day, another cup of #coffee"
api.update_status(status)
if __name__ == "__main__":
main()
这就是我得到的:
Authentication Successful
Traceback (most recent call last):
File "/Users/arnon/Downloads/mail_read/post_tweet.py", line 34, in <module>
main()
File "/Users/arnon/Downloads/mail_read/post_tweet.py", line 30, in main
api.update_status(status)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tweepy/api.py", line 1121, in update_status
return self.request(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tweepy/api.py", line 257, in request
raise Unauthorized(resp)
tweepy.errors.Unauthorized: 401 Unauthorized
在推特社区找到这个:
在创建您的应用程序时,请不要单击"创建您的访问令牌",直到您首先单击"设置"并将应用程序类型更改为"读、写和访问直接消息"。 在您的Twitter应用程序更新为"读、写和访问直接消息"后,单击Home选项卡,然后"创建您的访问令牌"。 这应该会起作用=)来源:https://twittercommunity.com/t/error-401-unauthorized/367/13