<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
  <post>
    <author>Simon Schoeters</author>
    <content>&lt;p&gt;As I had some troubles finding out how to show some of my own Flickr photos on this site using the Flickr API and Ruby on Rails I thought it would be useful to write it down in a small tutorial.&lt;/p&gt;
	
&lt;h3&gt;STEP 1: Finding a library: rFlickr&lt;/h3&gt;

&lt;p&gt;Flickr lists 3 possible Ruby frameworks for their API. After trying &lt;a href="http://redgreenblu.com/flickr/" title="My first shot at a Flickr API kit" class="ext"&gt;flickr.rb&lt;/a&gt; and not being able to login I decided to switch to &lt;a href="http://rubyforge.org/projects/rflickr/" title="The Flickr API kit I choose" class="ext"&gt;rFlickr&lt;/a&gt; as it seems the most popular one. I didn't look at the last one: &lt;a href="http://premshree.seacrow.com/code/ruby/flickr-ruby" title="Another Flickr API kit for Ruby" class="ext"&gt;flickr-ruby&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Start with installing the rFlickr library. You may need to uninstall a previous library if you installed another one earlier.&lt;/p&gt;

&lt;samp&gt;# gem install rflickr&lt;/samp&gt;

&lt;h3&gt;STEP 2: Get a Flickr API key&lt;/h3&gt;

&lt;p&gt;Flickr doesn't use the username/password combination as most other API's do. An application can ask for permissions which the user has to grant. A token is returned when this is done allowing the application to connect the next time so we'll only have to do this once. &lt;a href="http://flickr.com/services/api/keys/apply/" title="Request an API key" class="ext"&gt;Apply for a key&lt;/a&gt; at the Flickr website and remember the key and the shared secret. I use a &amp;ldquo;Web Application&amp;rdquo; key as I want to use it for my blog.&lt;/p&gt;

&lt;h3&gt;STEP 3: Get a Flickr token&lt;/h3&gt;

&lt;p&gt;Start your development environment:&lt;/p&gt;

&lt;samp&gt;# ./script/console&lt;/samp&gt;

&lt;p&gt;Next load the newly installed rFlickr module and ask for a Flickr frob using the API key and shared secret you got earlier.&lt;/p&gt;

&lt;samp&gt;&amp;gt;&amp;gt; require 'flickr'&lt;br /&gt;
=&amp;gt; []&lt;br /&gt;
&amp;gt;&amp;gt; flickr = Flickr.new(&amp;apos;/tmp/flickr.cache&amp;apos;, YOUR_KEY, YOUR_SECRET)&lt;br /&gt;
=&amp;gt; #&amp;lt;Flickr:0x...&lt;br /&gt;
&amp;gt;&amp;gt; flickr.auth.login_link&lt;br /&gt;
=&amp;gt; &amp;quot;http://flickr.com/services/auth/?api_sig=a07a7...&lt;/samp&gt;

&lt;p&gt;This will return a URL you need to copy &amp;amp; paste in your browser to allow the application to read, write and delete your photos. If you don't need all this permissions your can add the &amp;lsquo;perms&amp;rsquo; parameter for a less greedy permissions. Use the following if you only need read access to the photos:&lt;/p&gt;

&lt;samp&gt;&amp;gt;&amp;gt; flickr.auth.login_link(perms=&amp;apos;read&amp;apos;)&lt;/samp&gt;

&lt;p&gt;Allow the application to access your photos when you open the URL. You can request the forb we need and make sure to to keep it for now.&lt;/p&gt;

&lt;samp&gt;&amp;gt;&amp;gt; flickr.auth.getFrob
=&amp;gt; &amp;quot;7215760...&lt;/samp&gt;

&lt;p&gt;Now we&amp;apos;ll need a token. The token is more permanent as the frob but we need the forb to get one. Finally cache the token:&lt;/p&gt;

&lt;samp&gt;&amp;gt;&amp;gt; flickr.auth.getToken(&amp;apos;YOUR_FROB&amp;apos;)&lt;br /&gt;
=&amp;gt; #&amp;lt;Flickr::Token:0x...&lt;br /&gt;
&amp;gt;&amp;gt; flickr.auth.cache_token&lt;br /&gt;
=&amp;gt; 154&lt;/samp&gt;

&lt;h3&gt;STEP 4: Give me my photos!&lt;/h3&gt;

&lt;p&gt;Now it's time to do something more useful with the token. What about showing the thumbnails of the 6 last added photos? We'll need your NSID which is the strange number in your Flickr URL that looks like 26227936@N00.&lt;/p&gt;


&lt;samp&gt;def flickr
	require &amp;apos;flickr&amp;apos;&lt;br /&gt;
	photos = Array.new&lt;br /&gt;
	flickr = Flickr.new(&amp;apos;/tmp/flickr.cache&amp;apos;, &amp;apos;YOUR_KEY&amp;apos;, &amp;apos;YOUR_SECRET&amp;apos;)&lt;br /&gt;
	photo_list = flickr.people.getPublicPhotos(&amp;apos;YOUR_NSID&amp;apos;,&amp;apos;&amp;apos;,6)&lt;br /&gt;
	photo_list.each do |photo|&lt;br /&gt;
		photos.push( {&amp;apos;id&amp;apos; =&amp;gt; photo.id, &amp;apos;source&amp;apos; =&amp;gt; photo.sizes[:Square].source} )&lt;br /&gt;
	end&lt;br /&gt;
	return photos&lt;br /&gt;
end&lt;/samp&gt;

&lt;p&gt;Look, you now have an array with the last 6 posted public photos and their ID. Good luck!&lt;/p&gt;

&lt;h3&gt;Resources&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.gemjack.com/gems/rflickr-2006.02.01/index.html" title="RDoc for the rFlickr library" class="ext"&gt;RDoc for the rFlickr library&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
    <created-at type="datetime">2007-10-05T00:42:51+02:00</created-at>
    <id type="integer">7</id>
    <location-id type="integer">1</location-id>
    <permalink>rflickr</permalink>
    <title>My Flickr photos with RoR</title>
    <updated-at type="datetime">2008-05-20T12:05:59+02:00</updated-at>
  </post>
</posts>
