python - Twython returned a 400 (Bad Request) when tweeting an image -
i'm writing small script in python running in raspbian tweet speedtest result , i'm trying attach image, code:
import sys, urllib, cstringio pil import image twython import twython ... file = cstringio.stringio(urllib.urlopen(url_img_).read()) imagen = image.open(file) print imagen image_ids = twitter.upload_media(media=imagen) print image_ids twitter.update_status(status=message_, media_ids=[image_ids['media_id']])
but getting error on twitter.upload_media:
traceback (most recent call last): file "twitter_phy.py", line 29, in <module> image_ids = twitter.upload_media(media=imagen) file "/usr/local/lib/python2.7/dist-packages/twython/endpoints.py", line 140, in upload_media return self.post('https://upload.twitter.com/1.1/media/upload.json', params=params) file "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 269, in post return self.request(endpoint, 'post', params=params, version=version) file "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 259, in request api_call=url) file "/usr/local/lib/python2.7/dist-packages/twython/api.py", line 198, in _request retry_after=response.headers.get('x-rate-limit-reset')) twython.exceptions.twythonerror: twitter api returned 400 (bad request), media parameter missing.
this first approach python, please :(
woops... saving file first made trick:
## download image f = open('speedtest.png','wb') f.write(urllib.urlopen(url_).read()) f.close() ## read saved image imagen = open('speedtest.png', 'rb') ## upload image_ids = twitter.upload_media(media=imagen) ## tweet it! twitter.update_status(status=message_, media_ids=[image_ids['media_id']]) sys.exit()
Comments
Post a Comment