Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Rolling your own Flickr libs was never *this* easy
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
History.txt
LICENSE
README.rdoc
Rakefile
VERSION.yml
simpleflickr.gemspec

README.rdoc

Introduction

Made as an abstraction to the Flickr API in an extensible way. Where other libraries fully implement the API, this one doesn't. This library signs your requests and performs the HTTP request. Furthermore, it provides you an easy way to authenticate your users.

Usage

SimpleFlickr allows you to do authenticated and non-authenticated requests. If you only plan to do non-authenticated requests, skip the section on authentication and read on at “Example request”.

In either case, assume we have the following class defined:

class SomeClient < SimpleFlickr::Base
  api_key 'my_key'
  secret  'my_secret'
end

Authentication

For doing authenticated requests you first need to request a token for the user. SimpleFlickr implements two methods of Flickr authentication, being web- and desktop-authentication.

You can read up on the process at www.flickr.com/services/api/misc.userauth.html

For both methods, you need to generate an URL for the user to visit and authenticate your app. When generating this URL, you can specify what access level you need. This can be either :read, :write, or :delete, and defaults to :read.

Web Authentication

web_auth = SomeClient.web_authentication
web_auth.authentication_url(:write)   
# => "http://www.flickr.com/services/auth/?..."

When the user authenticated your app, he/she get redirected to your callback URL. You can pass this method either the frob in the URL (params[:frob]), or the entire URL (request.request_uri). Assuming this is not part of your original request/response cycle, you may need to re-instantiate the WebAuthentication object.

web_auth = SomeClient.web_authentication
token = web_auth.get_token(params[:frob])

Desktop authentication

desktop_auth = SomeClient.desktop_authentication
desktop_auth.authentication_url(:write)
# => "http://www.flickr.com/services/auth/?..."

When the user authenticated your app, he/she needs to inform your app that the authentication is complete. Then your app can continue requesting a token from Flickr that allows you to perform authenticated requests.

This method uses the previously generated frob that was stored in the DesktopAuthentication object to request a token. If you delete this instance in between, you can specify the frob to request a token for by passing it as an argument.

So, if your desktop_auth object is still alive, invoke:

token = desktop_auth.get_token

Otherwise, first store the frob after generating the authentication URL, and pass it to the get_token method later:

frob_to_store = desktop_auth.frob

# The app quits here, or you do some other magic.

desktop_auth = SomeClient.desktop_authentication
token = desktop_auth.get_token(frob_that_you_stored_somewhere)

Example request

Ofcourse, if we do non-authenticated requests (like searching all publicly available photos), we don't need to use a token. This would look like so:

client = SomeClient.new
client.get 'photos.search' do |params|
  params.tags = ['drinking', 'beer'].join(',')
end

While this is quite self-explanatory, I'll elaborate a bit. You can call the #get method with the action to call (refer to the Flickr API which methods are available). As all Flickr methods start with flickr., you can leave that out for readability. If you specify a block, you can define parameters for the call. Any parameter can be called as an accessor.

The method returns a Hpricot XML object. For a full reference to traversal of this object, please look into the Hpricot docs. But to given you an impression:

doc = client.get 'photos.search' do |params|
  params.tags = ['drinking', 'beer'].join(',')
end

(doc / 'photo').collect { |p| [p[:title], p[:id]] }
# => [["DSC09104", "3193174165"], ["DSC09130", "3193173953"], ...]

Example authenticated request

Making an authenticated request differs from non-authenticated requests in one way only, being that you need to specify the token to use. This can be done in a number of ways.

Instantiate your client with the token:

client = SomeClient.new(token)

Instantiate a client without a token and specify it when doing a method-call:

client = SomeClient.new
client.with_token(token) do
  get 'photos.search' do
    ...
  end
end

Don't instantiate and use the provided class-method:

SomeClient.with_token(token) do
  get 'photos.search' do
    ...
  end
end

It is even possible to scope calls to a token like this:

client.with_token(token_of_user1) do
  # stuff for user1
  with_token(token_of_user2) do
    # other stuff for user2
  end
  # some more stuff for user1
end

An example call for getting all private photos for a user would look like this:

SomeClient.with_token(token_of_user) do
  get 'photos.search' do |params|
    # Look into the Flickr API docs for a full reference of possible parameters
    # http://www.flickr.com/services/api/flickr.photos.search.html
    params.user_id = :me
    params.privacy_level = 5
  end
end

Author

  • Pieter Noordhuis (pcnoordhuis@gmail.com)

License

Copyright © 2008 Pieter Noordhuis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.