Javascript 必须使用活动访问令牌来查询有关当前用户的信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26709869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An active access token must be used to query information about the current user
提问by sigil
I'm trying to output my Facebook feed to a div element.
我正在尝试将我的 Facebook 提要输出到 div 元素。
Using this code:
使用此代码:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'APP ID HERE',
xfbml : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
}
else {
console.log('initiate FB login...');
FB.login();
}
});
FB.api('/me/feed',function(response){
var idDiv=document.getElementById('result');
idDiv.textContent=JSON.stringify(response);
});
};
(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";
console.log(js.length);
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div id='result'></div>
I get "logged in" displayed on the console, so I know that FB.getLoginStatus()has returned connected.
我在控制台上显示“登录”,所以我知道FB.getLoginStatus()已经返回connected.
The divelement gets populated with the following error:
该div元素填充以下错误:
{"error":{"message":"An active access token must be used to query information
about the current user.","type":"OAuthException","code":2500}}
If FB.getLoginStatus()returned connected, shouldn't there be an active access token?
如果FB.getLoginStatus()返回connected,不应该有一个活动的访问令牌吗?
回答by jvanstry
You probably don't have the access token at the time the request to 'me/feed' is sent.
在发送对“我/提要”的请求时,您可能没有访问令牌。
getLoginStatus does not only check if the user is authorized, it also refreshes the user session. that′s why it is important to do api calls after it is "connected".
getLoginStatus 不仅检查用户是否被授权,它还刷新用户会话。这就是为什么在“连接”之后进行 api 调用很重要的原因。
Try putting the call to FB.api inside getLoginStatus.
尝试将调用 FB.api 放在 getLoginStatus 中。
This question is similar to the problem you're having:Facebook javascript sdk An active access token must be used to query information about the current user
这个问题类似于您遇到的问题:Facebook javascript sdk An active access token must be used to query information about the current user

