2

I would like to use Facebook Score API (to set and read user and friends score) using facebook Android SDK.

Facebook documentation says:

Read score for a player

You can read the score for people playing your game by issuing an HTTP GET request to /USER_ID/scores with a user or app access_token.

[https://developers.facebook.com/docs/games/scores/][1]

However this documentation is far too little, and there is no sample to be found about this. There is one android sample found in facebook android SDK (friendsmash) who is featuring a leaderboard, but this sample is using its own server instead of the Score API.

Does anyone can please give us some samples or more detailed explanations about how we can do this?

user2802618
  • 21
  • 1
  • 2

2 Answers2

2

Code snippets for use Score API in Android are as below.

For Post score:(Call after login success)

final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "", "Please wait");
Bundle param = new Bundle();
param.putInt("score", 11000);
Request request = new Request(Session.getActiveSession(), "me/scores", param , HttpMethod.POST);
request.setCallback(new Request.Callback()
{
    @Override
    public void onCompleted(Response response)
    {
        Log.d("log_tag", "response: " + response.toString());
        dialog.dismiss();
    }
});
request.executeAsync();

For retrive all scores for application:(Call after login success)

final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "", "Please wait");
Request request = new Request(Session.getActiveSession(), "<app_id>/scores", null, HttpMethod.GET);
request.setCallback(new Request.Callback()
{
    @Override
    public void onCompleted(Response response)
    {
        Log.d("log_tag", "response: " + response.toString());
        dialog.dismiss();
    }
});
request.executeAsync();

Please follow this link for more detail: https://developers.facebook.com/docs/games/scores/

Kalpesh
  • 1,767
  • 19
  • 29
1

There is a more detailed example on the facebook developer blog but it is in php. I agree though the documentation seems to be lacking.

Source: https://developers.facebook.com/blog/post/539/

The following PHP example demonstrates how to access the signed_request parameter, app access token and prompt the user to authorize your app the publish_actions permission:

<?php
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$canvas_page_url = 'YOUR_CANVAS_PAGE_URL';

// Authenticate the user
session_start();
if  (isset($_REQUEST["code"])) {
$code = $_REQUEST["code"];
}

if(empty($code) && !isset($_REQUEST['error'])) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = 'https://www.facebook.com/dialog/oauth?' 
  . 'client_id=' . $app_id
  . '&redirect_uri=' . urlencode($canvas_page_url)
  . '&state=' . $_SESSION['state']
  . '&scope=publish_actions';

print('<script> top.location.href=\'' . $dialog_url . '\'</script>');
exit;
} else if(isset($_REQUEST['error'])) { 
// The user did not authorize the app
print($_REQUEST['error_description']);
exit;
};

// Get the User ID
$signed_request = parse_signed_request($_POST['signed_request'],
$app_secret);

$uid = $signed_request['user_id'];
echo 'Welcome User: ' . $uid;

// Get an App Access Token
$token_url = 'https://graph.facebook.com/oauth/access_token?'
. 'client_id=' . $app_id
. '&client_secret=' . $app_secret
. '&grant_type=client_credentials';

$token_response = file_get_contents($token_url);
$params = null;
parse_str($token_response, $params);
$app_access_token = $params['access_token'];

function parse_signed_request($signed_request, $secret) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

// decode the data
$sig = base64_url_decode($encoded_sig);
$data = json_decode(base64_url_decode($payload), true);

if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
  error_log('Unknown algorithm. Expected HMAC-SHA256');
  return null;
}

// check sig
$expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
if ($sig !== $expected_sig) {
  error_log('Bad Signed JSON signature!');
  return null;
}

return $data;
}

function base64_url_decode($input) {
return base64_decode(strtr($input, '-_', '+/'));
}
?>

Publishing User Scores

https://graph.facebook.com/USER_ID/scores?
score=USER_SCORE&access_token=APP_ACCESS_TOKEN
ClintL
  • 1,424
  • 14
  • 30