Prasad Surase
Prasad Surase

Reputation: 6574

retrieve facebook album images in rails 4 using koala and omniauth-facebook

I am using rails 4.2 with omniauth-facebook to signup using facebook and planning to use koala to retrieve current_user's facebook data(albums, friend list etc). At user signup/login, I am creating/updating the user details as below

def self.from_omniauth(auth)
  # Get 60 days auth token
  oauth = Koala::Facebook::OAuth.new(Rails.application.secrets.facebook['app_id'], Rails.application.secrets.facebook['app_secret'])
  new_access_info = oauth.exchange_access_token_info auth.credentials.token

  new_oauth_token = new_access_info['access_token']
  new_oauth_expires_at = DateTime.now + new_access_info['expires'].to_i.seconds

  where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|                                                                          
    user.provider = auth.provider
    user.uid = auth.uid
    user.name = auth.info.name
    user.password = Devise.friendly_token[0,20]
    #user.image = auth.info.image
    user.email = auth.info.email
    user.oauth_token = new_oauth_token
    user.oauth_expires_at = new_oauth_expires_at
    user.save!
  end
end

As per the Graph API section at koala, we need to specify the access token, which we can get through Facebook's Graph API Explorer. I created an access token for my application with the permissions to retrieve user details, likes, friends and photos.

Some console outputs:

pry(main)> a.class
=> User
pry(main)> a.oauth_token
=> "CAAHTERFDSFSREWrWErwelrinwoerxwerxqw"
pry(main)> graph = Koala::Facebook::API.new a.oauth_token
=> #<Koala::Facebook::API:0x00000008198810 @access_token= "CAAHTERFDSFSREWrWErwelrinwoerxwerxqw", @app_secret=nil>
pry(main)> friends = graph.get_connections("me", "friends")
=> {...}
pry(main)> photos = graph.get_connections("me", "photos")
=> {...}
pry(main)> photos.count
=> 25
pry(main)> p = graph.get_object "me"
=> {"id"=>"10206089",
   "email"=>"[email protected]",
   "first_name"=>"Prasad",
   "gender"=>"male",
   "last_name"=>"Surase",
   "link"=>"https://www.facebook.com/app_scoped_user_id/10206089/",
   "locale"=>"en_US",
   "name"=>"Prasad Surase",
   "timezone"=>5.5,
   "updated_time"=>"2014-12-14T06:12:07+0000",
   "verified"=>true}

My questions is that can we retrieve current_user's photos that have been created in a specific date range. Is this possible? I need the image title, image likes and image comments for each image.

Upvotes: 1

Views: 848

Answers (1)

Olivier Lance
Olivier Lance

Reputation: 1748

You can use the since and until query parameters to specify a date range:

/me/photos/uploaded?since=1402733742&until=1408004142

Using Koala, I guess this should be done like so:

photos = graph.get_connections("me", "photos/uploaded", since: 1402733742, until: 1408004142)

This returns all my photos from 14/06/2014 to 14/08/2014.

Be careful though: from what I have read, Facebook seems to limit the date range. You might get an error saying you tried to get data from too many days at once and hence, be ready to slice your date range in chunks to perform your request. My own tests using the graph explorer showed that Facebook paginates the results and return cursors that allow pagination. You should be able to use next_page on the collection Koala returns to go through all results.

Upvotes: 1

Related Questions