Livecommunity powered by sixgroups.com
  ABOUT MASHUP CAMP WIKI BEST MASHUP CONTEST NEWS SPONSORS CONTACT TV BLOG WHO'S COMING?

RoR and Flickr Mashup

From MashupCamp

Jump to: navigation, search

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

Contents

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

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 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/rails_flickr.zip

Next, make these changes:

Edit environment.rb

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

# Include your application configuration below
MY_CONFIG = {
  #mashupcampdemo.us:3000
  :google_key => "ABQIAAAArk3wBzn5p0hIazFBq9_qyhQstSMu_qPO-oFtKKydax4RznMAdhQ-tLyoKvVImG8y6T5WvGJZmGE8QA",
  :flickr_cache_file => "config/flickr.cache",
  :flickr_key => "3c10ddd41ae761c27d8dd516459ded57",
  :flickr_shared_secret => "640c5c5ce47ffa8b",
  :flickr_id => "29281775@N00",
  :rflickr_lib => true
}  

Getting a Flickr API Key

The above config file uses my Flickr API key. Here is how to get your own key. Start by going to:

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 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={}, ....

>> `start #{f.auth.login_link('write')}`
=> ""

A browser window will now appear. Confirm that you allow access. Then go back to the command window:

>> 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

Advanced Flickr

You now have access to Flickr and have a whole world of possibilities open to you on how to use it. You might notice that there is some drag-and-drop functionality in the code, so maybe you could build a program that allows you to drag-and-drop pictures into a wiki. Or you could search for all pictures with certain tags and show them dynamically on your web site. Let's see some cool applications that you build with this!