下面的代码是我的Facebook API javascript
到目前为止。它非常简单,用一堆console.log()
来跟随代码流。(我认识到命名参数response和response2是一个糟糕的做法,但这只是我现在快速放置的,以避免命名冲突)。
目前,FB.init()
成功,FB.getLoginStatus()
工作,FB.api()
两次调用工作,但FB.login()
从未发生。它是异步的,所以console.log()
就在do execute之前和之后。总而言之,我的控制台输出是:
在这里
让它也在这里
权限:对象{data: Array[37]}很高兴见到你,my-name-is-here。
我猜这是因为异步FB.login()
调用从未得到响应,所以回调从未触发,因此没有console.log()
s从函数内部。但是我需要这个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()在用户已连接时被调用。"
但是我认为当用户已经登录时获得更多权限的方法是通过再次调用FB.login()
,在参数参数中指定所需的权限。
您的流程不正确。首先检查FB.api("/me/permissions")
回调中的权限(因为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
});
}
尝试在你的FB.init中使用oauth:true。
如果你在发布FB.login之前还没有必要的权限,你应该通过代码检查。