Skip to content

Instantly share code, notes, and snippets.

@blairg23
Last active January 21, 2022 14:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blairg23/dc421453b035321a5e27 to your computer and use it in GitHub Desktop.
Save blairg23/dc421453b035321a5e27 to your computer and use it in GitHub Desktop.
Oauth Example Authentication with Flickr using requests_oauthlib in Python
import requests_oauthlib
api_key = <api_key_here>
api_secret = <api_secret_here>
# OAuth URLs
request_token_url = 'https://www.flickr.com/services/oauth/request_token'
access_token_url = 'https://www.flickr.com/services/oauth/access_token'
authorization_url = 'https://www.flickr.com/services/oauth/authorize'
callback_uri = <callback_uri_here>
oauth_session = requests_oauthlib.OAuth1Session(client_key=api_key, client_secret=api_secret, signature_method=u'HMAC-SHA1', signature_type=u'AUTH_HEADER', callback_uri=callback_uri)
# First step, fetch the request token:
request_token = oauth_session.fetch_request_token(request_token_url)
# Second step, follow this link and authorize:
print oauth_session.authorization_url(authorization_url)
# Third step, fetch the access token:
redirect_response = raw_input('Paste the full redirect URL here.')
print oauth_session.parse_authorization_response(redirect_response)
print oauth_session.fetch_access_token(access_token_url)
# Done! You can now make authenticated requests using the token and token secret.
@ZuluPro
Copy link

ZuluPro commented Mar 13, 2016

It saves my day, thanks !

@yihuangwilliam
Copy link

Hello, I tried to use your code. But I can't understand the redirect_response. What is the full redirect URL? I used my callback_uri again, but failed like the following.

https://www.flickr.com/services/oauth/authorize?oauth_token=72157668148760831-21176a0ae4bb4a5c
Paste the full redirect URL here.https://yihuangportfolio.wordpress.com/
Traceback (most recent call last):
File "D:\Eclipse Workspace\Flickr\src\oauth.py", line 23, in
print (oauth_session.parse_authorization_response(redirect_response))
File "D:\Python3.5.1\lib\site-packages\requests_oauthlib\oauth1_session.py", line 328, in parse_authorization_response
self._populate_attributes(token)
File "D:\Python3.5.1\lib\site-packages\requests_oauthlib\oauth1_session.py", line 337, in _populate_attributes
token,
requests_oauthlib.oauth1_session.TokenMissing: Response does not contain a token: {}

@blairg23
Copy link
Author

blairg23 commented Aug 3, 2016

Is that the redirect URL you specified when you created your API key?

@dkirkby
Copy link

dkirkby commented Sep 12, 2016

This works great, although I had to append &perms=read (or write / delete) to oauth_session.authorization_url(authorization_url) in order to obtain a valid authorization link.

@camslice
Copy link

camslice commented Jul 2, 2017

Nice work! You've made this process as simple as I think it can possibly be.

Python 3+ needs a few tweaks to get it working:

  • print statements require parenthesis around the entire value
  • raw_input has been renamed to input

@tomquirk
Copy link

tomquirk commented Sep 24, 2017

Hey all,

If you have the urge, check out the Python Flickr API. Consult the wiki for steps similar to the ones on this gist for getting tokens and authenticating!

@blairg23 - would love to get your code in the project. Your method is the best I've found. Consider a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment