Skip to content

Flickr API Keys and Authentication

Tom Quirk edited this page Sep 24, 2017 · 6 revisions

Flickr API Keys and Authentication

To use the Flickr API, you will need:

  • a Flickr account;
  • an API key, and;
  • a secret key

API Keys

To retrieve the necessary keys, refer to the Flickr instructions

flickr_api provides two ways to use the keys within your application:

  1. Set the key information using the set_keys function.
import flickr_api
flickr_api.set_keys(api_key = 'my_api_key', api_secret = 'my_secret')

or...

  1. Create a flickr_keys.py settings module within the flickr_api directory or available from your PYTHON_PATH. It should look like:
API_KEY =  'my_api_key'
API_SECRET = 'my_secret'

After this, you will be able to use the Flickr API for anything that does not require user authentication.

Authentication

If your application needs to modify data or to access non-public material, it will require an authentication token. The AuthHandler object is designed to deal with the authentication process.

To get an authentication token, the AuthHandler will ask an authorisation request. It will then provide an URL at which the user should be redirected so he can grant the required permissions. At the end of the authorisation process, a verifier code will be provided. This code must be given to the AuthHandler object which will then be able to get the authentication token.

Here is the simplest recipe for getting an authentication token.

  1. Set the API Keys using one of the methods above. Then, perform the following steps in a Python interpretter:
>>> a = flickr_api.auth.AuthHandler() # creates a new AuthHandler object
>>> perms = "read" # set the required permissions
>>> url = a.get_authorization_url(perms)
>>> print url # this is the url we need!
  1. Copy the printed URL into a web browser and, if prompted, login to Flickr and accept any requested permissions. You will be redirected to an XML page.

Locate the oauth_verifier tag. The content of this tag is the verifier that needs to be given to the AuthHandler.

  1. Copy the oauth_verifier tag into the set_verifier function and run the following lines in a Python interpretter:
>>> a.set_verifier("the verifier code") # copy your oauth_verifier tag here!
>>> flickr_api.set_auth_handler(a) # set the AuthHandler for the session

At this point, your session should be authorised! To save having to do this process again, save the authorisation details to a file for later use:

>>> a.save(filename)

Next time you want to access your account you just need to reload it:

>>> flickr_api.set_auth_handler(filename)

Finally, your application code might look something like this:

flickr.set_keys(api_key="YOUR_API_KEY",
                api_secret="YOUR_SECRET_KEY") # not this line if you store these details in flickr_keys.py
flickr.set_auth_handler(".auth.txt") # or whatever you save your auth file as

Permission request callback URL

Note that the URL you are redirected to after you have successfully granted the permissions (step 2) is called the "callback" URL. The XML page you have seen corresponds to the default callback URL. Within a web application, you are supposed to redirect the user to Flickr for the authorisation process. Once it is completed you would like the user to be redirected to a URL on your own website which will read automatically the verifier code in the query part of the URL. This can be done through the "callback" argument of the AuthHandler constructor. In this case, you will want to replace the second line above by something like:

a = flickr_api.auth.AuthHandler(callback = "http://www.mysite.com/get_verifier/")