Skip to content

Instantly share code, notes, and snippets.

@cwurld
Created April 29, 2013 18:18
Show Gist options
  • Save cwurld/5483567 to your computer and use it in GitHub Desktop.
Save cwurld/5483567 to your computer and use it in GitHub Desktop.
How to POST a photo to Flickr using Python The Flickr api docs are OK, but lacking in detail about how to post a photo once you complete the oauth process. Matt Kelsey has a nice blog post about basic oauth process with Flickr: http://mkelsey.com/2011/07/03/Flickr-oAuth-Python-Example/. Read his blog first. I should mention that I tried using Py…
import time
import oauth2 as oauth
import requests # From: http://docs.python-requests.org/en/latest/
# oauth_token and oauth_token_secret are the tokens produced at the end of the Flickr oauth process
token = oauth.Token(oauth_token, oauth_token_secret)
photo_url = 'http://api.flickr.com/services/upload'
# Fill in your own app KEY and SECRET
consumer = oauth.Consumer(key=FLICKR_API_KEY, secret=FLICKR_API_SECRET)
data = {
'oauth_consumer_key': consumer.key,
'oauth_nonce': oauth.generate_nonce(),
'oauth_signature_method':"HMAC-SHA1",
'oauth_timestamp': str(int(time.time())),
'oauth_token': token.key,
'oauth_version': 1.0,
}
req = oauth.Request(method="POST", url=photo_url, parameters=data)
signature = oauth.SignatureMethod_HMAC_SHA1().sign(req, consumer, token)
req['oauth_signature'] = signature
# Here is where the docs(http://www.flickr.com/services/api/upload.api.html) are vague. The docs say
# that the field "photo" should be "The file to upload". The code below implements that statement.
files = {'photo': ('test.jpg',open('/path/to/my/file/test.jpg', 'rb'))}
r = requests.post(photo_url, data=req, files=files)
print 'Status ',r.status_code
print 'text ', r.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment