[PYTHON] OAuth authentication using requests Example: Flickr

Easily authenticate to Flickr with OAuth using requests and requests_oauthlib "Example of hitting WebAPI using requests: Flickr" I hit Flickr's WebAPI, but in reality it is a private photo in my account I often want to access it. Therefore, I will show you how to make your account accessible using OAuth authentication.

install Since it is registered on PyPi, install it using pip or easy_install.

$ pip install requests requests_oauthlib

Get API key

If you don't have a Flickr account, get one as needed.

Access the following address, register the application, and obtain the API Key and SECRET. For non-commercial registration, register with Non-Commercial. https://www.flickr.com/services/apps/create/apply/

OAuth authentication

Sample code

The Flickr OAuth procedure is explained on the official website. This time we are targeting Flickr, but basically the procedure should be the same for OAuth1. Flickr:User Authentication

import urlparse
import webbrowser

import requests
from requests_oauthlib import OAuth1


API_KEY = 'YOUR_API_KEY'
SECRET_KEY = 'YOUR_SECRET_KEY'

request_url = 'https://www.flickr.com/services/oauth/request_token'
authorize_url = 'https://www.flickr.com/services/oauth/authorize'
access_token_url = 'https://www.flickr.com/services/oauth/access_token'
access_url = 'https://api.flickr.com/services/rest/'
callback_uri = 'oob'


def oauth_requests():
    # Get request token
    auth = OAuth1(API_KEY, SECRET_KEY, callback_uri=callback_uri)
    r = requests.post(request_url, auth=auth)
    request_token = dict(urlparse.parse_qsl(r.text))

    # Getting the User Authorization
    webbrowser.open('%s?oauth_token=%s&perms=delete' % (authorize_url, request_token['oauth_token']))  #Open the browser and display the OAuth authentication confirmation screen. If the user permits, the PIN code will be displayed.

    oauth_verifier = raw_input("Please input PIN code:")  #Enter the above PIN code
    auth = OAuth1(
        API_KEY,
        SECRET_KEY,
        request_token['oauth_token'],
        request_token['oauth_token_secret'],
        verifier=oauth_verifier)
    r = requests.post(access_token_url, auth=auth)

    access_token = dict(urlparse.parse_qsl(r.text))
    return access_token

if __name__ == '__main__':
    print oauth_requests()

This will give you an Access token and an Access token secret, which you should be able to use to access your account.

On another occasion regarding the API specifications after authentication ...

reference

-Flickr Official User Authentication

-How to authenticate with OAuth using super convenient Python module requests and work with Zaim

Recommended Posts

OAuth authentication using requests Example: Flickr
Download images using requests
Example of using lambda
Save images using python3 requests