gpt4 book ai didi

javascript - FB.api 不是每次都工作

转载 作者:行者123 更新时间:2023-11-28 21:13:34 25 4
gpt4 key购买 nike

我是 Facebook Javascript SDK 的新手。我正在尝试使用此 API 创建一个应用程序。但在使用 FB.api 方法时运气不佳。不知何故,我的 FB.api 方法每次都无法正常工作。有时它会给出响应(我只是访问response.name),而其他时候它不会正确调用。

window.fbAsyncInit = function() {
FB.init({
appId : 'my_app_id', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.authResponse) {
alert('user is:'+response.status);
userId = response.authResponse.userID;
FB.api('/me',function(response) {
alert('Inside FB.api function');
document.getElementById("userInfo").innerHTML = 'Welcome <img src="https://graph.facebook.com/'+ response.id + '/picture" style="margin-right:5px"/>' +response.name + '!';
});
}
});
};

其他方法 FB.ui 和 FB.getLoginStatus 以及 Facebook 之类的按钮都按预期工作,但只有 FB.api 没有像其他方法那样无缝工作。不为什么?请帮我找出问题所在。

最佳答案

我遇到了完全相同的问题。这个答案为我指明了正确的方向:

FB.api does not work when called right after FB.init

为了解决我的问题,我必须订阅正确的事件。就我而言,它是 FB.init 之后的“auth.authResponseChange”

FB.Event.subscribe('auth.authResponseChange', function(){
FB.api('/me',function(r){
console.log(r.id);
if (!r || r.error) {
alert('Error occured');
} else {
alert(r.name);
}
});
});

关于javascript - FB.api 不是每次都工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com