Search code examples
ruby-on-railsruby-on-rails-3facebook-graph-apikoalafacebook-checkins

Friends checkins with facebook graph api and rails - APIError


I am trying to get a users friends checkins, I am using omniauth and koala gem.

When a user gets saved this method hits:

def add_friends
 friends_data = facebook.get_connections("me", "friends", :fields => "id, name, link, picture, gender, checkins")
     friends_data.map do |h|
        friend = Friend.new
        friend.uid = h["id"]
        friend.name = h["name"]
        friend.image = h["picture"]
        friend.gender = h["gender"]
        friend.urls = h["link"]
        friend.user_id = self.id
        friend.save!

        if (!h["checkins"].blank?)
           checkin = Checkin.new
           checkin.checkin_id = h["id"]
           checkin.user_id = h["checkins"]["data"]["from"]["id"] 
           checkin.user_name = h["checkins"]["data"]["from"]["name"] 
           checkin.tags = h["checkins"]["tags"]["data"]["name"]
           checkin.place_id = h["checkins"]["place"]["id"]
           checkin.place_name = h["checkins"]["place"]["name"]
           checkin.message = h["checkins"]["message"]
           checkin.created_time = h["checkins"]["created_time"]
           checkin.friend_id = friend.id
           checkin.save!
           end
         end
      end

But I get this error:

Koala::Facebook::APIError: HTTP 500: Response body: {"error_code":1,"error_msg":"An unknown error occurred"}

I dont really know what that means, any ideas? And does anybody know how to define a limit on checkins with the koala gem? I tried something like this:

u.facebook.get_connections("me","friends", :fields => "checkins.limit(2)")

But I got the same error!


Solution

  • In fields, you're requesting information about a friend, but 'checkins' isn't a profile field, it's an entirely different connection altogether.

    What you must do is loop through all the friend IDs and get the checkins for each:

    friend_checkins = []
    friend_ids = u.facebook.get_connections("me","friends", :fields => "id")
    friend_ids.each do |friend_id|
        friend_checkins << u.facebook.get_connections(friend_id,"checkins")
    end
    

    Also, when doing this, it would be a great time to look into batch requests with Koala, as you could potentially be making a lot of calls to the API here..