fb的。登录响应始终返回true
-
12-12-2019 - |
题
为了检查suer是否给予了所有需要的权限,我这样做:
FB.login(function(response){
console.log(response.status);
if (response.status == 'connected') {
/* user gave permssions */
}else{
/* user didnt, unmark the checkbox */
$('input:checkbox').removeAttr('checked');
}
}, { scope: 'publish_stream' });
问题是,这是allways返回true,如果用户无关紧要:登录,ommits或关闭弹出窗口。
知道为什么吗?
也试过:如果(响应。authResponse){没有成功。.
解决方案
这里的问题是 publish_stream
是一个 扩展权限, ,这意味着用户可以选择退出该权限。一般来说,当用户点击回调中的代码块时,他们已经验证了您的应用程序,但不一定具有您要求的所有权限,因为其中一些可以是 扩展权限. response.status
仅用于传达用户是否已验证应用程序的状态,而不是他们是否已接受您请求的所有对话框提示/权限。在你的情况下, publish_stream
是一个扩展权限,因此您不能保证在回调中对用户具有该权限。如果你要求 publish_stream
作为用户已经通过身份验证后的增量权限,然后您的条件检查 response.status
将始终返回true(因为根据定义,用户已经验证了您的应用程序)。
如果你想验证你有 publish_stream
在回调中,检查使用的权限 /me/permissions
图api上的端点。
你想要的是这样的:
FB.login(function(response){
if (response.status == 'connected') {
FB.api('/me/permissions', function(response) {
var permsArray = response.data[0];
// Permissions that are needed for the app
var permsNeeded = ['publish_stream'];
var permsToPrompt = [];
for (var i in permsNeeded) {
if (permsArray[permsNeeded[i]] == null) {
permsToPrompt.push(permsNeeded[i]);
}
}
if (permsToPrompt.length > 0) {
$('input:checkbox').removeAttr('checked');
}
}
} else {
/* user didnt, unmark the checkbox */
$('input:checkbox').removeAttr('checked');
}
}, { scope: 'publish_stream' });
其他提示
我不知道为什么,但以下代码至少对我工作良好〜
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo FACEBOOK_APP_ID ?>',
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.getLoginStatus(function(response){
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
var signed_request = response.authResponse.signedRequest;
// avoid using cookie
self.location= "<?php echo site_url()?>/signup/fb_login/"+uid;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
FB.login(function(response) {
if (response.authResponse) {
self.location="<?php echo site_url()?>/signup/fb_register";
/* FB.api('/me', function(response) { */
/* }); */
} }, {scope: 'email,user_hometown'});
} else { // unknown
// the user isn't logged in to Facebook.
}
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
.
`
不隶属于 StackOverflow