Facebook Leaderboard - Get Json Instead Of Webview

Using a combination of Jon Beebe’s 2-3 year old tutorial and some posts I’ve discovered on here, I’ve finally managed to setup a rough Facebook Leaderboard. However as this post claims, I’m trying to get hold of the JSON data and use within my app as opposed to showing everything in a web view like Beebe’s tutorial outlines.

 

The mentioned post talks about modifying the PHP scripts to get hold of the JSON as opposed to putting the data to HTML, however I haven’t got a clue how to handle this. I believe I need to look at the PHP script that handles the HTML output, but how my lua file gets hold of this I’m not sure.

 

Any help would be appreciated.

Hey, SegaBoy, have you looked into Facebook Score API?  Facebook returns JSON object with the list of FB friends who posted score, and it’s already sorted by highest score to the lowest score.  All I do is just display it based on the JSON data I get from FB.

 

When posting my own high score, I use this:

 

[lua]

local attachment = {

    score = tostring(myhighscore),

    access_token=mytoken

}

facebook.request(“me/scores” , “POST”, attachment)

[/lua]

 

When retrieving JSON object with list of friends and their scores, I simply call:

[lua]facebook.request(appID … “/scores”)[/lua]

 

The response I get from FB would be handled by json.decoding the event.response:

[lua]

local response = {}

response = json.decode( event.response ) 

[/lua]

 

You might want to print the event.response on terminal/console and see what it prints.  It should give you enough information as to how you may want to make use of the JSON object that FB sends you back.

 

I hope this helps and is relevant to what you are trying to do…

 

Naomi

Hi Naomi - so from what I can surmise from your post and the Facebook dev page it seems as though I didn’t need to go to the lengths of setting up a server, database, php scripts, etc… *MAJOR FACEPALM* :) 

 

Am I right in assuming that you send a score to Facebook and they’ll handle it - no need for us developers to have an external database? Then I just handle the requests like you’ve shown above?

 

Can’t believe how easy this might be and if so how stupid I’ve been re-inventing the wheel :wink:

Naomi - just before I shut down my server and scrap my PHP scripts - I just wanted to make sure this approach would serve the purpose I’m intending - which is imply a leader board showing a user’s FB friends scores?

 

Many thanks,

Hey, SegaBoy, yes, I use it to show the leader board of FB friends.  If your friends are not posting score to Facebook from your app, they won’t show up.  So if you are the only person in the circle of FB friends that post score using FB score API from your app, FB will send back JSON object with your score data and nothing else.  However, as soon as your friends start posting their score, his/her score will come back along with yours.

 

One thing I noticed, though, is that after I delete and reinstall the app and post a new high score to FB, even if the new high score is actually lower than the one posted before reinstalling the app, FB will only record/retain the most recent score posted, which is different from how it works with iOS Game Center.

 

BTW, you don’t have to shut down your server and do away with your PHP script.  Why don’t you give FB score API a shot, and if it doesn’t work the way you envision, you can always resume with your server work.

 

Naomi

Naomi, you’ve piqued my interest in having my next game post scores to Facebook.  If I get this correctly, it’s not posting a status update that you’ve scored, but there’s an actual scoreboard on Facebook that is automatically sorted and retrievable from a Facebook request?

 

Has anyone posted direct instructions or steps on how to get this done?  Does this need an Open Graph or special App settings in Facebook’s developer pages?  I saw your code and I see your passing a variable called mytoken.  I don’t see any reference here on what it is or how to get it.

 

I think I’ve done simple posting and uploading photos a while back, but this seems like something new (at least for me) that I’d like to try to integrate.  Can you provide some direction or more of your code?  Thanks.

Hey, BeyondtheTech, when your app connects to FB (definitely when app invokes facebook.login, and maybe when facebook.request is fired, but I’m not 100% sure), you get a event.token back from FB.  That’s the token you want.  

 

If you’ve done simple posting and uploading photos before, it shouldn’t be all that difficult to get FB Score API working.  Remember to set the permissions correctly at FB dev page for your app.  publish_streams permission is now deprecated. You need to use publish_actions instead.  Also, you can no longer post messages on friends wall unless you use facebook.showDialog function.

 

Naomi

Thanks.  I’ll give it a shot.  But, I’m hoping someone could come up with a complete run-through for Corona’s Facebook integration.  It seems like it’s gone through a few iterations, but the documentation is in pieces over various sections throughout the site (and off the site) over the last few years.

@BeyondTheTech -agreed, wish CL would allow some kind of public access on the documentation side. I can understand not opening it up entirely, that only works if you’ve got a lot of people keeping an eye over things. However what Naomi has said would have saved me a day!

Also, I read on another forum thread that you can’t use

"me/scores"

but you have to use

userID .. "/scores"

.  Which is correct?  Very sparse and confusing stuff here.

facebook.request(“me/scores” , “POST”, attachment) works fine with my app.  But then, I can also imagine how  userID … “/scores”  may work too.  I suppose it depends on how you set things up.

 

Naomi

Plugging in my iPhone to Xcode’s Organizer, I see this when it’s trying to perform the high score post.

I'm getting Error: HTTP status code 400 The operation couldn't be completed. (com.facebook.sdk error 5.)

When I had those, I googled “HTTP status code 400”.   If I remember correctly, I think it has something to do with making FB request with wrong syntax and/or parameter.

 

Naomi

 

Edit:  What daily build are you using?  There was FB SDK update integration issues with earlier daily builds.  I don’t remember which one fixed it, but it was definitely fixed by 1041.

I’m on the latest 1055.

@beyondthetech, then, if I were you, I’d look at the attachment table and see if there’s any syntax error there.

 

Edit:  I’d also double check on the FB dev center and make sure all settings, including the permissions, are properly set.  And remember, it takes time to propagate after you save the app on FB.

 

Edit2:  Also, if you are making facebook.request that is no longer allowed by FB, you could get an error too.

 

Naomi

All of the documentation should have links at the bottom that lets you give a thumbs up or a thumbs down and report problems  like outdated and incorrect, incomplete entries.  I’m not sure the guides have that and I know the blogs don’t.

 

You can always email support AT coronalabs DOT com for the things that don’t have the reporting feature.

 

As for Jon’s blog.  That was probably written before we had a Facebook API library that uses their Single Sign on facilities and may be why you have to jump through all of that web hosting stuff.

I can’t speak for everyone, but I think most of the main features of Facebook integration are well understood by most of us now, but it’s just the few things like loading a scoreboard and posting a score that really isn’t documented anywhere here, unless I just can’t find it.

 

Considering that most of Corona’s developers are game developers, I would figure this would be more discussed or “advertised,” so to speak.  Also, I’m not sure what settings or permissions are exactly required on Facebook’s Developers page, and there was no mention of scoreboards in any of the settings pages there, and any possibly-related permission settings are vague enough for me not to know if I need to set it one way or the other.

the problem i had with posting facebook scores was solved after i  realized you needed to post the score as a STRING!

Looks like it didn’t grab the token and user ID properly, so yeah, it would definitely have come back as an Error 400.  I’ve cobbled this code to get the necessary info I wanted:

function faceListener( event ) if event.isError then -- problem with Facebook connection else if event.type == "session" and event.phase == "login" then -- successful log in if event.token and facebookToken == nil then facebookToken = event.token print( "Facebook Access Token captured: " .. facebookToken ) end -- while we've logged in, circle around and grab personal info facebook.request( "me" ) elseif ( event.type == "request" ) then local response = event.response print( "Facebook unparsed response: " .. response ) if event.isError then -- error detected in request else response = json.decode( event.response ) if response.id and facebookUserID == nil then facebookUserID = response.id print( "Facebook User ID captured: " .. facebookUserID ) end if response.username then print( "Username: " .. response.username ) end if response.name then print( "Full Name: " .. response.name ) end if response.first\_name then print( "First Name: " .. response.first\_name ) end end end end end

 

Now, it comes back with a {“FACEBOOK_NON_JSON_RESULT”:“true”} when I post the high score, but I can’t find it anywhere in my timeline or activity.  Almost there, but something’s amiss.  Did you have to make any particular changes on the App’s details in the Facebook Developer page for it to work?

@Naomi: By any chance have you experimented with the Achievements API? Score would be great in that it’s extremely simple, but I had the idea of hiding the actual points. I.e. rather go with “you achieved 92% of the high score”, “you beat the high score”, which is not possible with Score but would be with Achievements (as far as I see). That said capabilities of Achievements are far greater than my needs, so I’m afraid I’m entering a world of hurt with it :slight_smile: