Reading and settings scores to facebook app / game and my Adobe’s FB API update

March 22nd, 2012 by Slav

Majority of the calls you would ever make to Facebook API require user access token. There are some however, that require app access token instead. These are normally calls you do on behalf of the app not user. An example of such situation would be creating a test users, or reading Insight data.

To my surprise app access token is also required when you want to read or set user scores.

For those who are not aware of this, you can set and read score value for an app user.  For this to work, you have to set the type of your application to “Game”, in the FB developers app general settings page.

As current Adobe’s Facebook API didn’t support making such calls, I modified the source code (for web development) so that it would.

Btw, if anybody from the Adobe’s Facebook API administrators is reading this, please feel free to include these changes in your next version. You can download the modified source  here.

So how do you use this ? Here is the example how to set and read user scores, and also how to read scores of the users’s friends that have played the app/game.

First of all in addition to app id you will also need to pass an application secret to the init function.

protected static const APP_ID : String = "YOUR APP ID GOES HERE";
protected static const APP_SECRET : String = "YOUR APP SECRET GOES HERE";

Facebook.init(APP_ID, handleInit, null, null, APP_SECRET);

App will now initialise and function handleInit() will only be called after app access token is received. Now you are ready to make calls that require app access token.

Let’s set some user scores first :

private function setScore() : void
{
     var randomScore : int = Math.random() * 10000; //just generate some random score number
     var params : Object = {score:randomScore};
     Facebook.api("/" + userFB_ID + "/scores", onScoreSubmitted, params, "POST", true);
}

notice the 5th argument, which is a boolean switch “use app access token” set to true. By default it is false. Without this call would use user access token and the whole operation would fail.

userFB_ID is facebook user id, which you can get from the auth object at the login callback, or anytime by calling Facebook.getAuthResponse().uid

Ok, this is how you can read scores of the user that’s currently logged in :

private function readScore() : void
{
     Facebook.api("/" + userFB_ID + "/scores", onScoresRead, null, null, true);
}
private function onScoresRead(response : Object, fail : Object) : void
{
 var username : String = response[0].user.name;
 var appName : String = response[0].application.name;
 var score : String = response[0].score;
 trace(username + " score for app " + appName + " is " + score);
}

Again as with setting the score, reading also requires fifth argument be set to true. Response object will contain array of score object for different games (if user played more then one game) .. in my example I am assuming just one game.

Very useful functionality is finding out the score values for user friends that have also played the game. This is useful for building leader boards for the game that has friends scores on it as it makes it more personal and motivate user to beat his friends – thus play your game more. For this to work you have to ask a permission : “friends_games_activity” at the login stage.

Here’s the script :

private function readAppScores() : void
{
    Facebook.api("/" + APP_ID + "/scores", onAppScoresRead);
}
private function onAppScoresRead(response : Object, fail : Object) : void
{
 if (response)
 {
 var players_arr : Array = response as Array;
 output("High Scores are : \n");
 for (var i : int = 0; i < players_arr.length; i++)
 {
 var username : String = players_arr[i].user.name;
 var score : String = players_arr[i].score;
 trace(username + " score is " + score+"\n");
 }
 }
}

Interestingly for this functionality you have to use user access token not app access token. The callback returns array of objects containing information about app, user and scores.

Posted in AS3, Facebook, flash, Games

24 Responses

  1. Armand

    Thank you very much for the modified Facebook API code. I hit a roadblock with the APP secret but now with your code it works just fine!

  2. Armand

    I read more into this issue and I came to the conclusion that the approach is wrong.

    From the FB dev:
    As Application Access Tokens are built off the Games client secret, which is a shared secret between Facebook and the app, these should not be stored client side to protect their integrity. We will keep our Application Access Token on our web server and write a small PHP script that will POST scores.

    So that’s why there is no support for App Access Tokens in the Facebook API – because it requires us to embed the App Secret inside the swf, which would pose a security risk.

    The only proper way to do this is server-side.

  3. flash games

    I really like what you guys are usually up too.
    This kind of clever work and coverage! Keep up the good works guys I’ve
    included you guys to our blogroll.

  4. Ardis

    Thanks designed for sharing such a nice idea,
    paragraph is fastidious, thats why i have read it entirely

  5. Aracelis

    I am not positive where you’re getting your information, but good topic.
    I must spend a while learning much more or understanding more.
    Thank you for fantastic info I used to be in search of this information for my mission.

  6. sears auto fort worth hulen

    I’ve been exploring for a bit for any high-quality articles or weblog posts in
    this sort of space . Exploring in Yahoo I at last stumbled upon this web site.
    Studying this information So i am happy to
    express that I have an incredibly just right uncanny feeling I came upon just what I needed.
    I most surely will make sure to don?t omit this site and give it a glance on a continuing basis.

  7. dieting and breastfeeding

    Have you ever considered publishing an ebook or guest authoring
    on other websites? I have a blog centered on the same subjects you discuss and would love to have you share some
    stories/information. I know my viewers would enjoy
    your work. If you are even remotely interested, feel free to send
    me an e mail.

  8. mortgage payment tips tricks

    I have learn several just right stuff here. Definitely worth bookmarking for revisiting.
    I surprise how much attempt you put to create any such wonderful
    informative site.

  9. photo tips rainy day

    Good way of telling, and nice paragraph to get information on the topic of my presentation focus, which i am going to deliver in
    academy.

  10. Refugia

    I’m very happy to uncover this site. I want to to thank you for your time just for this wonderful
    read!! I definitely really liked every little bit of it and I
    have you saved as a favorite to see new things on your site.

  11. online free Education

    You actually make it seem so easy with your presentation but I find this matter to be really something that I think I would
    never understand. It seems too complicated and extremely broad for me.
    I am looking forward for your next post, I will try to get the hang of
    it!

  12. animal cell

    I’m really enjoying the design and layout of your website.

    It’s a very easy on the eyes which makes it much more enjoyable for me
    to come here and visit more often. Did you hire out a designer to create your theme?
    Fantastic work!

  13. gratuit Pokemon X et Y Version downloader gratuitement

    Thanks for sharing your thoughts about idézetek.
    Regards

  14. Les Sims 3 En Route Vers Le Futur Telecharger

    If you are going for most excellent contents like myself, simply pay
    a quick visit this web site daily for the reason that it presents
    feature contents, thanks

    Feel free to surf to my web blog; Les Sims 3 En Route Vers Le Futur Telecharger

  15. phx smokeless cigarettes reviews

    Have you ever considered about including a little bit more than just
    your articles? I mean, what you say is fundamental and all.

    Nevertheless think about if you added some great graphics or videos to give your posts more,
    “pop”! Your content is excellent but with pics and video clips, this blog
    could undeniably be one of the best in its field.
    Terrific blog!

  16. Unlock iOS 7

    It is a very cost effective way to develop ‘cross platform’,
    and dramatically reduces the cost of maintaining apps in the future.
    The app also lets you share your edited videos and HD movies via various video-sharing websites like Vimeo, You – Tube and social networking sites like Facebook.
    I’ve been using these headphones while doing my running chores on the treadmill or while jogging outdoors.

  17. http://myaldridgeapps.com/?a=original+weight+loss+a+hrefhttpwww.alumniaffairs.ait.ac.thnode597150www.alumniaffairs.ait.ac.tha

    By making the brain believe that there is a band around
    the stomach, weight loss is imminent. Although I did a bunch
    of strength related exercises at the gym such as
    squats, leg presses, leg extensions and curls I never felt the
    power that I was able to get from contracting my muscles against gravity in
    a sustained fashion. Weight-loss lacks to get difficult, try
    our tips to see precisely how productive you could be.

  18. Les Zévadés de l'espace Télécharger Le Film

    Hey there I am so thrilled I found your weblog, I really found you by mistake, while I was looking on Askjeeve for something else,
    Anyways I am here now and would just like to say many thanks for a tremendous post and a
    all round thrilling blog (I also love the theme/design), I don’t have time to look over it
    all at the minute but I have saved it and also added in your RSS feeds, so when I have time I
    will be back to read a great deal more, Please do keep up the
    superb work.

    Visit my website Les Zévadés de l’espace Télécharger Le Film

  19. the childrens place coupons

    What’s up it’s me, I am also visiting this website daily, this
    site is really fastidious and the visitors are actually sharing fastidious thoughts.

  20. kindle fire hd review

    Heya i’m for the first time here. I found this board and I
    to find It really helpful & it helped me out much.
    I am hoping to present one thing back and aid others like you
    aided me.

  21. weight loss zija

    Hello Dear, are you in fact visiting this site regularly, if
    so after that you will without doubt get fastidious knowledge.

  22. nyc hvac

    Whats up are using WordPress for your site platform? I’m new to the blog world
    but I’m trying to get started and set up my own. Do you
    need any html coding expertise to make your own blog?
    Any help would be greatly appreciated!

  23. luxor telecharger gratuit,

    I’ve been surfing online more than 2 hours today, yet I never
    found any interesting article like yours. It is pretty worth enough for me.
    In my view, if all site owners and bloggers
    made good content as you did, the internet will be a lot more useful than ever before.

    Feel free to visit my site; luxor telecharger gratuit,

  24. http://www.rwclassconnect.com

    Jeremy Gislason is a leading expert on membership sites,
    marketing and online business. For issues of the real device
    the developer has only one option of buying the Android Phone called Dev1 especially those located outside US.

    I loved either one of these video games systems, because that
    they had video games that have been amusing to play.