My Flickr photos with RoR

Adding photos to my website via the Flickr API.

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.

gem install rflickr

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.

>> require 'flickr'
=> []
>> flickr ='/tmp/flickr.cache', YOUR_KEY, YOUR_SECRET)
=> #<Flickr:0x...
>> flickr.auth.login_link
=> ''

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.

>> flickr.auth.login_link(perms='read')

Allow the application to access your photos when you open the URL. You can request the forb we need.

>> flickr.auth.getFrob
=> '7215760...'

Next, we’ll need a token. The token is more permanent than the frob, but we need the forb to request the token.

>> flickr.auth.getToken('YOUR_FROB')
=> #<Flickr::Token:0x...
>> flickr.auth.cache_token
=> 154

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.

def flickr
  require 'flickr'
  photos =
  flickr ='/tmp/flickr.cache', 'YOUR_KEY', 'YOUR_SECRET')
  photo_list = flickr.people.getPublicPhotos('YOUR_NSID','',6)
  photo_list.each do |photo|
    photos.push( {'id' =>, 'source' => photo.sizes[:Square].source} )
  return photos

You now have an array with the 6 most recent public photos and their IDs.