Skip to content

Instantly share code, notes, and snippets.

@michaelhelmick
Created January 12, 2012 23:22
Show Gist options
  • Save michaelhelmick/1603772 to your computer and use it in GitHub Desktop.
Save michaelhelmick/1603772 to your computer and use it in GitHub Desktop.
Upload Image with Twython
def updateProfileImage(self, filename, version = 1):
""" updateProfileImage(filename)
Updates the authenticating user's profile image (avatar).
Parameters:
image - Required. Must be a valid GIF, JPG, or PNG image of less than 700 kilobytes in size. Images with width larger than 500 pixels will be scaled down.
version (number) - Optional. API version to request. Entire Twython class defaults to 1, but you can override on a function-by-function or class basis - (version=2), etc.
"""
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(length=41),
'oauth_timestamp': int(time.time()),
}
#create a fake request with your upload url and parameters
faux_req = oauth.Request(method='POST', url='http://api.twitter.com/1/account/update_profile_image.json', parameters=params)
#sign the fake request.
signature_method = oauth.SignatureMethod_HMAC_SHA1()
faux_req.sign_request(signature_method, self.consumer, self.token)
#create a dict out of the fake request signed params
params = dict(parse_qsl(faux_req.to_postdata()))
self.headers.update(faux_req.to_header(realm='http://api.twitter.com'))
print self.headers
import requests
r = requests.post('http://api.twitter.com/1/account/update_profile_image.json', files={'image':('avvy100411.jpg', open(filename, 'rb'))}, headers=self.headers)
print r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment