Wednesday, May 8, 2013

Facebook Scores API with Koala Gem

Here is a code snippet for a piece of code that I found a lot of people asking about on the web. Since Koala gem does not have an explicit API for Facebook Scores API, some people wonder why it isn't there.

Actually, there is a generic call that can do the job just fine:

After getting your Koala gem to work: check this URL for more about Creating a Facebook Rails app with Koala gem.


Read the set of scores for a user and their friends

Facebook Scores API states that: "You can read the set of scores for a user and their friends for your app by issuing an HTTP GET request to /APP_ID/scores with the user access_token for that app."

So this can be done with the 'get_object()' method,
api = Koala::Facebook::API.new(session[:access_token])
scores = api.get_object(APP_ID + "/scores")


Create or update a score for a user:

Facebook Scores API states that: "You can post a score for a user by issuing an HTTP POST request to /USER_ID/scores with a user or app access_token as long as the user has granted the publish_actions permission for your app."

So this can be done with the 'put_connections()' method,
api = Koala::Facebook::API.new(session[:access_token])
user_profile = api.get_object("me")
result = api.put_connections(user_profile['id'], 'scores?score='+my_score)

if result == true
    #great!
else
    #oops!
end



No comments:

Post a Comment