Twython fails to upload image and responds with "Twitter API returned a 400 (Bad Request)"

298 Views Asked by At

Twython version 3.8.2 Python: 3.6.8

I can't seem to upload a picture as part of a Tweet. I am getting a 400 Bad Request error, but I cannot determine how or why. I can send tweets with the "update_status(msg)" call without issue so I know the credentials are fine. The picture file to upload is local and readable (rw-rw-rw) Is there a way to get at more detail from the response that comes back from Twitter?

    twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token_key,
    access_token_secret)


    message="Hello World"
    photo = open('/tmp/beach.jpg')
    response = twitter.upload_media(media=photo)
    twitter.update_status(status=message, media_ids=[response['media_id']])

    Traceback (most recent call last):
      File "twitter2.py", line 31, in <module>
        response = twitter.upload_media(media=photo)
      File "/home/me/python-virtual-enviros/slack_api/lib64/python3.6/site-packages/twython/endpoints.py", line 162, in upload_media
        return self.post('https://upload.twitter.com/1.1/media/upload.json', params=params)
      File "/home/me/python-virtual-enviros/slack_api/lib64/python3.6/site-packages/twython/api.py", line 282, in post
        return self.request(endpoint, 'POST', params=params, version=version, json_encoded=json_encoded)
      File "/home/me/python-virtual-enviros/slack_api/lib64/python3.6/site-packages/twython/api.py", line 272, in request
        api_call=url, json_encoded=json_encoded)
      File "/home/me/python-virtual-enviros/slack_api/lib64/python3.6/site-packages/twython/api.py", line 204, in _request
        retry_after=response.headers.get('X-Rate-Limit-Reset'))
    twython.exceptions.TwythonError: Twitter API returned a 400 (Bad Request), An error occurred processing your request.
1

There are 1 best solutions below

0
Sean D On

The file was corrupt. With a good file, the code above works fine and no error returned.