Tweepy is throwing an error when I try to upload an image. (AttributeError: 'dict' object has no attribute 'media_id_string')

Tweepy is throwing an error when I try to upload an image. (AttributeError: 'dict' object has no attribute 'media_id_string')

import tweepy
import os
from PIL import Image
consumer_key = "hidden"
consumer_secret = "hidden"
access_token = "hidden"
access_token_secret = "hidden"


auth = tweepy.OAuth1UserHandler(
   consumer_key, consumer_secret, access_token, access_token_secret
)

api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())



# Upload images and get media_ids
filename = "test01.jpg"
media = api.media_upload(filename)


# Tweet with multiple images
api.update_status(status='Test 1 image', media_ids=[media.media_id_string])

我得到的错误是 -

Traceback (most recent call last):
  File "/Users/anildhage/Coding-Workspace/twitter-bot/imdb bot/twitter_test.py", line 26, in <module>
    api.update_status(status='Test 1 image', media_ids=[media.media_id_string])
AttributeError: 'dict' object has no attribute 'media_id_string'
ERROR conda.cli.main_run:execute(33): Subprocess for 'conda run ['python', '/Users/anildhage/Coding-Workspace/twitter-bot/imdb bot/twitter_test.py']' command failed.  (See above for error)

我看了很多文章和 youtube 视频,上面的代码有效但不适合我。我在这里做错了什么?

由于您使用的是 JSON 解析器,因此 media 成为一个字典对象,因此您应该像这样访问它:

api.update_status(status='Test 1 image', media_ids=[media['media_id_string']])

如果您希望将其用作 属性,只需从您的 tweepy.API 调用中删除 parser=...

api = tweepy.API(auth)