401未经授权的错误在使用Tweepy发布推文时发生。

huangapple go评论68阅读模式
英文:

401 Unauthorized error when posting tweets using Tweepy

问题

我正在使用 tweepy 发布包含媒体的推文,用于 一个机器人项目。自从六月中旬以来,机器人已经停止工作,我意识到这是由于 Twitter API 的更新导致的。

我将我的 conda 环境中的 tweepy 升级到版本 4.14.0。我更新了我的脚本以使用 API v1.1 上传媒体,并使用 API v2 发布推文。我重新生成了消费者密钥、消费者密钥秘密、访问令牌和访问令牌秘密。

当我尝试使用 API v2 发布推文时,我收到了 401 未授权 的错误。凭据是正确的,因为在发布推文之前,API v1.1 成功上传媒体。

我的应用程序已在一个项目下注册,并具有读写权限。

我在 Twitter 开发者论坛上搜索了一下,找到了一些类似的错误,但没有解决方案。我在那里发布了 一条消息,但没有收到任何答复。

以下是我的 Python 代码:

"""用于测试 s2coastalbot 推文发布的脚本。
"""

# 第三方库导入
import tweepy

# 验证 Twitter 帐户
consumer_key = "XXX"
consumer_secret = "XXX"
access_token = "XXX"
access_token_secret = "XXX"
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_token_secret)
apiv1 = tweepy.API(auth)
apiv2 = tweepy.Client(consumer_key, consumer_secret, access_token, access_token_secret)

# 发布推文
file_path = "media.png"
media = apiv1.media_upload(filename=file_path)
apiv2.create_tweet(text="test", media_ids=[media.media_id], user_auth=True) # 返回: "tweepy.errors.Unauthorized: 401 Unauthorized"
英文:

I am using tweepy to post tweets including media, for a bot project. Since mid-june the bot has stopped working, and I realized it’s due to an update of the twitter APIs.

I upgraded tweepy in my conda environment to version 4.14.0. I updated my script to used API v1.1 to upload media, and API v2 to post the tweet. I regenerated the consumer key, consumer secret, access token and access token secret.

I am getting a 401 Unauthorized error when trying to post the tweet with API v2. The credentials are correct since API v1.1 succeeds in uploading the media pior to tweet posting.

My app is registered under a project and has read and write permissions.

I search the twitter developer forum, found some similar errors but no solution. I posted a message there but didn't get any answer.

Here is my python code:

"""Script to test tweet posting of s2coastalbot.
"""

# third party imports
import tweepy

# authenticate twitter account
consumer_key = "XXX"
consumer_secret = "XXX"
access_token = "XXX"
access_token_secret = "XXX"
auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_token_secret)
apiv1 = tweepy.API(auth)
apiv2 = tweepy.Client(consumer_key, consumer_secret, access_token, access_token_secret)

# post tweet
file_path = "media.png"
media = apiv1.media_upload(filename=file_path)
apiv2.create_tweet(text="test", media_ids=[media.media_id], user_auth=True) # returns: "tweepy.errors.Unauthorized: 401 Unauthorized"

答案1

得分: 0

I was passing the wrong arguments to Client().
Without using a bearer_token, I needed to pass my credentials using keyword arguments.
The following syntax works:

apiv2 = tweepy.Client(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret)
英文:

I was passing the wrong arguments to Client().
Without using a bearer_token, I needed to pass my credentials using keyword arguments.
The following syntax works:

apiv2 = tweepy.Client(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret)

huangapple
  • 本文由 发表于 2023年7月18日 03:16:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定