As I had some difficulties showing my photos on this site using the Flickr API and Ruby on Rails. I thought it useful to write it down in a small tutorial.
The right library
Flickr lists 3 possible Ruby frameworks for their API. After trying flickr.rb (no longer available) and not being able to login I decided to switch to rFlickr, it looks like the most popular one.
Start with installing the rFlickr library. You may need to uninstall earlier libraries if you installed one before.
Get a Flickr API key
Flickr doesn’t use the username/password combination as one would expect. A 3rd party application can ask the user for permissions. Flickr returns a token which the application can use to connect from then on. Apply for an API key via the Flickr website and remember the key and a shared secret. I used a “Web Application” key as I use it for this blog.
Get a Flickr token
Start your Rails development environment:
Next, load the newly installed rFlickr module and ask for a Flickr frob using the API key and shared secret you got earlier.
This will return a URL you need to copy & paste in your browser to allow the application to read, write and delete your photos. If you don’t need all these permissions, you can add the “perms” parameter to be less greedy. This snippet gets read access to the photos.
Allow the application to access your photos when you open the URL. You can request the forb we need.
Next, we’ll need a token. The token is more permanent than the frob, but we need the forb to request the token.
Give me my photos
Finally, we can do something useful with the token. What about showing the thumbnails of the 6 most recently added photos? We’ll need the NSID which is the strange number in your Flickr URL that looks like 26227936@N00.
You now have an array with the 6 most recent public photos and their IDs.