到目前为止,下面的代码是我的 Facebook API javascript
。它非常简单,有一堆 console.log()
来跟随代码流。 (我认识到命名参数 response 和 response2 是糟糕的做法,但这只是我现在为了避免命名冲突而快速提出的)。
就目前而言,FB.init()
成功,FB.getLoginStatus()
工作,并且两个 FB.api()
调用工作,但 FB.login()
永远不会发生。它是异步的,所以 console.log()
就在执行之前和之后。总而言之,我的控制台输出是:
makes it here
makes it here too
Permissions: Object {data: Array[37]} Good to see you, my-name-is-here.
我猜这是因为异步 FB.login()
调用永远不会得到响应,所以回调永远不会触发,因此没有 console.log()
来自内部功能。但我需要此 FB.login()
才能获得扩展权限 ads_management
。知道哪里出了问题,或者至少知道自己解决这个问题的方向吗?
window.fbAsyncInit = function() {
FB.init({
appId: 'my-app-id-is-here',
xfbml: true,
version: 'v2.0'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/me', function(response2) {
console.log("Good to see you, " + response2.name + ".");
});
FB.api('/me/permissions', function(response2) {
console.log("Permissions:");
console.log(response2);
});
console.log("makes it here");
FB.login(function(response2) {
console.log("why not here?");
if (response2.authResponse) {
console.log("Response to login asking for ads_management permission");
console.log(response2);
} else {
console.log("User cancelled login or did not fully authorize.");
}
}, {
scope: 'ads_management',
return_scopes: true
});
console.log("makes it here too");
} else {
// filled with other code that doesn't effect this question yet
}
});
};
更新:
去掉参数参数 {scope: 'ads_management', return_scopes: true}
至少可以尝试调用 FB.login()
,但在控制台中现在有错误:
"FB.login() called when user is already connected."
但我认为当用户已经登录时获得更多权限的方法是再次调用 FB.login()
,并在参数参数中指定所需的权限。
最佳答案
您的流程不正确。首先检查 FB.api("/me/permissions")
callback 中的权限(因为 facebook api 调用是异步的),然后检查该特定权限是否是尚未授予,则调用 FB.login()
。
给你-
if (response.status === 'connected'){
FB.api('/me/permissions', function(response){
var hasPermission = false;
var response = new Array();
response = response.data;
response.map(function (data) {
if (data.permission == "ads_management" && "status" == "granted") {
hasPermission = true;
}
});
if(!hasPermission)
// call FB.login() here
});
}
关于javascript - 用于 Javascript 的 Facebook SDK,FB.login() 回调未触发(异步调用永远不会得到响应?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24293058/