【问题标题】:Post tweets using tweepy and Twitter API V2使用 tweepy 和 Twitter API V2 发布推文
【发布时间】:2021-12-31 07:11:33
【问题描述】:

我需要我的 Twitter 应用程序来发布一些信息,但是出了点问题。 首先,我创建了一个应用程序并尝试使用此代码来测试凭据:

auth = tweepy.OAuthHandler("CONSUMER_KEY", "CONSUMER_SECRET")
auth.set_access_token("ACCESS_TOKEN", "ACCESS_SECRET")
api = tweepy.API(auth)
try:
    api.verify_credentials()
    print("Authentication Successful")
except:
    print("Authentication Error")

并得到“身份验证错误”。 然后我尝试直接使用

写一条推文
client = tweepy.Client(bearer_token, consumer_key, consumer_secret, access_token, access_token_secret)
client.create_tweet(text="********")

现在我得到“tweepy.errors.Forbidden: 403 Forbidden”我该怎么办?

【问题讨论】:

    标签: python api twitter tweepy tweets


    【解决方案1】:

    不可能首先确定到底发生了什么,因为您要抑制实际错误并在任何异常上打印它。您需要为任何人提供完整的追溯信息,以了解它发生了什么。

    但是,如果您对 Twitter API 具有基本访问权限,则将无法使用 Twitter API v1.1,并且会遇到 403 Forbidden 错误。 请参阅FAQ section about that in Tweepy's documentation 了解更多信息。

    对于后一种错误,请确保您的应用具有写入权限。
    请参阅FAQ section about that in Tweepy's documentation 了解更多信息。

    【讨论】:

      【解决方案2】:

      您想通过 V2 发布推文吗?这是我刚刚answered to myself的解决方案!

      安装tweepy,然后像我一样发推文“是的,男孩!我做到了”。

      !pip3 install tweepy --upgrade # to install and upgrade tweepy if you didn't.
      

      然后准备好你的 BEARER、CONSUMER_KEY、CONSUMER_SECRET、ACCESS_KEY 和 ACCESS_SECRET。如果您不知道如何找到它们,请查看Developer Platform -> Developer Portal -> Projects & Apps -> click on your project -> then look for "Keys and tokens"

      import tweepy
      
      client = tweepy.Client(bearer_token=BEARER, consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, access_token=ACCESS_KEY, access_token_secret=ACCESS_SECRET)
      
      client.create_tweet(text="Yeah boy! I did it")
      

      这对我来说是 100% 测试的。我仍然不知道我是否可以引用或回复带有 V2 的推文。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 2013-10-20
        • 2022-11-10
        • 2022-12-18
        • 2013-06-08
        • 2013-10-05
        • 2016-06-29
        相关资源
        最近更新 更多