我正在尝试创建一个机器人来从源中提取信息并每天以图像+文本的形式发布推文。
我知道 V1 端点将无法工作,但到目前为止我读到它仍然支持媒体上传。 V2 端点发送文本推文。
我已成功通过 V2 端点文本发送推文,但无法发送图像..
任何帮助将不胜感激。谢谢。
这是我的代码..我有错误(tweepy.errors.BadRequest:400错误请求您的媒体ID无效。)
def Tweet(self, date: str):
consumer_key = 'xxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
client = tweepy.Client(bearer_token='xxxxxxxxxxxxxxxxxxxxx',consumer_key= consumer_key,consumer_secret= consumer_secret,access_token= access_token,access_token_secret= access_token_secret)
# Authenticate with Twitter
auth = tweepy.OAuth1UserHandler(
consumer_key, consumer_secret, access_token, access_token_secret
)
api = tweepy.API(auth)
image_path = "C:\wex\pic.jpg"
with open(image_path, "rb") as image_file:
r1 = api.media_upload(filename='pic.jpg', file=image_file)
media_id = r1.media_id_string
media = api.media_upload(filename=image_path)
client.create_tweet(text="This Tweet IS A TEST SORRY!", media_ids=[media_id])
log.info("Posted!")
我尝试了很多行代码,但没有成功...总是出现错误..尝试了 ChatGPT 的帮助,但仍然有错误...