Home > Blockchain >  Tweepy is throwing an error when I try to upload an image. (AttributeError: 'dict' object
Tweepy is throwing an error when I try to upload an image. (AttributeError: 'dict' object

Time:03-28

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])

The error I'm getting is -

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)

I watch many articles and youtube videos, the above code works but not for me. What am I doing wrong here?

CodePudding user response:

Since you're using the JSON parser, media becomes a dict object so you should access it like this:

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

If you wish to use it as a property, simply drop the parser=... from your tweepy.API call:

api = tweepy.API(auth)
  • Related