2

我想使用 facebook Android SDK 使用 Facebook Score API(设置和读取用户和朋友的分数)。

Facebook 文档说:

读取玩家的分数

您可以通过使用用户或应用程序 access_token 向 /USER_ID/scores 发出 HTTP GET 请求来读取玩游戏的人的分数。

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

然而,这个文档太少了,并且没有关于这个的样本。在 facebook android SDK (friendsmash) 中发现了一个带有排行榜的 android 示例,但该示例使用自己的服务器而不是 Score API。

有没有人可以请给我们一些样本或更详细的解释我们如何做到这一点?

4

2 回答 2

2

在 Android 中使用 Score API 的代码片段如下。

对于帖子评分:(登录成功后调用)

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();

检索申请的所有分数:(登录成功后调用)

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();

请点击此链接了解更多详情:https ://developers.facebook.com/docs/games/scores/

于 2014-01-02T11:16:30.870 回答
1

facebook 开发者博客上有一个更详细的示例,但它是在 php 中。我同意,尽管似乎缺少文档。

来源:https ://developers.facebook.com/blog/post/539/

以下 PHP 示例演示了如何访问 signed_request 参数、应用访问令牌并提示用户向您的应用授权 publish_actions 权限:

<?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, '-_', '+/'));
}
?>

发布用户分数

https://graph.facebook.com/USER_ID/scores?
score=USER_SCORE&access_token=APP_ACCESS_TOKEN
于 2013-09-21T18:21:06.937 回答