I have a rails(ruby 2.2.1, rails 4.2.0) application that uses the koala gem (2.0.0) to retrieve all the images from all the albums for the user. The code for the facebook service is as
class FacebookService
attr_reader :graph
def initialize(token)
@graph = Koala::Facebook::API.new(token)
end
def photos
your_photos, photos_of_you = [], []
albums_ids.map do |album_id|
#this query will combine fetch all the photos in any album + comments + likes associated with that image
your_photos += graph.get_connections(album_id, "photos", {limit: 100, fields: ["id", "source", "images", "height", "width", "created_time", "likes", "comments{id, message, from{name, picture}, created_time}"]})
end
photos_of_you = graph.get_connections("me", "photos", {limit: 100, fields: ["id", "source", "images", "height", "width", "created_time", "likes", "comments{id, message, from{name, picture}, created_time}"]})
(your_photos + photos_of_you).flatten
end
def albums_ids
albums.map { |album| album["id"] }
end
def albums
graph.get_connections("me", "albums")
end
end
and has been used as
fb = FacebookService.new current_user.oauth_token
photos = fb.photos
I am saving those urls in the db for further reference. I tried saving the source and the largest image from the images array but the urls keep going invalid. The problem is that when the above code is executed, the urls in the source and images aren't valid after certain amount of time(say 2-3 days). How can I retrieve image url that don't break later?
Thanks.
I filed a bug about this one a while back - it appears it's by design, and that there's some reason for those expiring URLs. The solution here is to actually cache image data, rather than just a URL.