| Home | Business | Personal | Contact |
||
Max S. Dunn...when there is a will, there is a way |
||
In this lesson, we will learn how to integrate Flickr pictures into our web site.
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
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.
Now let’s put Flickr in our web site.
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:
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
}
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.
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!)
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:
Now that the flickr files have been added and environment.rb has been updated, restart WEBrick, and try it:
http://localhost:3000/flickr/show
In order to allow users to upload pictures to their account, you need to automate these steps:
Essentially, this is the same as section “9.2. Non-web based app” in http://www.flickr.com/services/api/auth.spec.html