无法使用Javascript API获取Facebook分数

时间:2013-11-11 16:04:16

标签: facebook facebook-graph-api

从facebook获得分数我有点问题。 我可以使用以下示例发布它们:

FB.api('/me/scores/', 'post', { score: myScore }, function(response) {
    console.log(response);
});

然后我尝试从facebook收到分数我得到OAuthException 104: “请求此资源需要访问令牌。”

我使用以下代码来获得分数:

FB.api('/MYAPPID/scores/', 'get', function(response) {
    console.log(response);
});

不应该由sdk本身发送access_token吗?

1 个答案:

答案 0 :(得分:2)

只需添加:FB.setAccessToken({USER_TOKEN});

示例:

FB.setAccessToken({USER_TOKEN});
FB.api('/me/scores/', 'post', { score: 1000 }, function(response) {
    console.log(response);
    FB.api('/{APP_ID}/scores/', 'get', function(response2) {
        console.log(response2);
    });
});

您可以使用USER_TOKEN = FB.getAuthResponse()['accessToken'];

获取userAccessToken

完整示例:

window.fbAsyncInit = function() {
    FB.init({
        appId      : '{YOUR_APP_ID}',
        cookie     : true,
        xfbml      : true,
        version    : 'v2.3'
    });
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            var access_token = FB.getAuthResponse()['accessToken'];
            FB.setAccessToken(access_token);
            FB.api('/me/scores/', 'post', { score: 1000 }, function(response) {
                console.log(response);
                FB.api('/{APP_ID}/scores/', 'get', function(response2) {
                    console.log(response2);
                });
            });
        }
    });
}
(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));