1

我在从 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);
});

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

4

1 回答 1

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

您可以通过以下方式获取 userAccessTokenUSER_TOKEN = FB.getAuthResponse()['accessToken'];

完整示例:

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'));
于 2015-05-05T09:43:17.173 回答