我如何授权Tweepy在Twitter上发帖? [英] How do I authorize Tweepy to post on Twitter?

查看:0
本文介绍了我如何授权Tweepy在Twitter上发帖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在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

这篇关于我如何授权Tweepy在Twitter上发帖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆