RoR and Flickr Mashup

In this lesson, we will learn how to integrate Flickr pictures into our web site.

rFlickr Library

Gem comes to the rescue and makes it easy to install the rFlickr library. Simply open a command window and type:

gem install rflickr

Answer “Yes” for any required dependencies.

If that doesn’t work, download the packages manually:

Unfortunately, there is not much documentation for rFlickr. Here is the API documentation which doesn’t help much:

http://gemjack.com/gems/rflickr-2006.02.01/index.html

Sometimes it is more helpful to look at the Flickr API since rFlickr is a direct mapping of this:

http://www.flickr.com/services/api

The Difference Between The ‘flickr’ And ‘rflickr” Libraries

There are two main Rails Flickr libraries available right now: flickr and rflickr. The plain flickr library is the one highlighted on [http://media.rubyonrails.org/video/flickr-rails-ajax.mov Putting Flickr on Rails]. However, this library doesn’t implement the full Flickr API and has some other bugs that make it difficult to use for advanced Flickr functions.

Now the problem with rflickr is that it has a bug that prevents you from using it in non-authorized mode, so you have to generate a token. Also the documentation is practically non-existent. But after studying the source code and playing with it, you can generally get it to do what you want, so that is what we will use here.

Rails Flickr Files

Now let’s put Flickr in our web site.

Main Files

First get the flickr rails files add them to your Rails home directory:

http://www.maxdunn.com/files/maxdunn/rails_flickr.zip

Note: On Linux or Mac, unzip these to a temporary directory first, then add them to your rails app directory. Otherwise, all of your other rails application files will be deleted!

Next, make these changes:

Edit environment.rb

Add this to the bottom of app/config/environment.rb

# Include your application configuration below
MY_CONFIG = {
  :flickr_cache_file => "#{RAILS_ROOT}/config/flickr.cache",
  :flickr_key => "3c10ddd41ae761c27d8dd516459ded57",
  :flickr_shared_secret => "640c5c5ce47ffa8b",
  :flickr_id => "29281775@N00",
  :rflickr_lib => true
}  

Getting a Flickr API Key

You can use the above Flickr API key or get your own with these instructions. First, get an API key at:

http://www.flickr.com/services/api/key.gne

(You may need to login to Flickr first.)

Be sure to select “For non-commercial use”, then go to the next screen.

Where it asks for “Authentication Type” select “Desktop Application”

Copy the key and secret to the environment.rb file. You should also find your Flickr ID and enter that in the config file too.

Getting Your Flickr ID

It turns out that getting your Flickr ID is not easy. However, there is a cool utility that can find it for you:

http://idgettr.com/”>http://idgettr.com

(Thanks to Ben Wisely for this tip!)

Generating a Flickr Token

Before you can use the Flickr API key, you must generate a token. We will use Ruby interactively, through the Ruby console:

C:\rails\demo>ruby script\console
Loading development environment.

>> f = MyFlickr.new
=> #<MyFlickr:0x3a59020 @blog_cache=nil, @group_by_id={}, ....

>> f.auth.getFrob
=> "4006181-547e8f82559635e1" 

>> u = f.auth.login_link('read')
=> "http://flickr.com/services/auth/?api_sig=3a031ccb711d58cc4fc1cd26576887cb&frob=3462763-e6bbacaeb35c9968&perms=write&
api_key=c1dd096037d33c250fa4dbe9d3a8f2a3" 

>> `start "title", "#{u}"`
=> "" 

The “start” command only works on Windows. For other systems, paste the url that appears in a browser window. (Note: there will be two lines, so you will need to paste the first line and then append the second line to it.) Confirm that you allow access. Then go back to the command window:

>> f.auth.getToken
=> #<Flickr::Token:0x3a7cda0 @token="121...

>> f.auth.cache_token
=> 137

Note: in the login_link command, the permissions are ‘read’, ‘write’, ‘delete’, in increasing order, i.e. ‘delete’ allows both read and write.

The key to this working without worrying about a callback and frob is to select “Desktop Application” for “Authentication Type” when generating the Flickr API key.

Here are some links that describe the process of generating a token:

Testing Flickr

Now that the flickr files have been added and environment.rb has been updated, restart WEBrick, and try it:

http://localhost:3000/flickr/show

Per User Authentication

In order to allow users to upload pictures to their account, you need to automate these steps:

  1. Get the users Flickr username and password
  2. Create the MyFlickr object passing in the username and password
  3. Get the frob with auth.getFrob
  4. Call auth.login_link(‘write’)
  5. Start a browser window with the returned URL so the user can okay the request
  6. Get the token with auth.getToken
  7. Store all this information separately for each user
  8. Pass this token and your api key when uploading the file

Essentially, this is the same as section “9.2. Non-web based app” in http://www.flickr.com/services/api/auth.spec.html