-
-
Save blairg23/dc421453b035321a5e27 to your computer and use it in GitHub Desktop.
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. |
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: {}
Is that the redirect URL you specified when you created your API key?
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.
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 valueraw_input
has been renamed toinput
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!
It saves my day, thanks !