我正在为我们网站上的facebook连接创建登录代码, 但是我找不到如何检查用户是否具有所需的权限。
使用旧的javascript,将为每个权限打开一个对话框,返回代码会说明是否接受了权限,如何使用javascript代码?
这是我到目前为止所获得的代码,在TODO中我想检查用户是否有权限
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: 'MY API KEY', status: true, cookie: true,xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
alert("logged in");
//TODO: check if all perms has been accepted!!!!
//if they have NOT been accepted, I want to logout the user
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, again, check if all perms has been accepted
alert("already logged in");
}
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
<fb:login-button perms="email,user_birthday,status_update,publish_stream" >Login with Facebook</fb:login-button>
顺便说一句,在他们有这个例子的文档中
http://developers.facebook.com/docs/reference/javascript/FB.login
他们使用自定义按钮,这就是为什么我怀疑fb会有类似的东西:loginbutton
答案 0 :(得分:2)
FB.Event.subscribe('auth.login',function(response) {
if(response.session) { //checks if session is true
alert('logged in');
if(response.perms) { //checks if perms is true = permissions are granted
alert('perms granted');
}
else { //if perms is false = no permissions granted
alert('no perms');
}
}
else { //if something goes wrong
alert('login failure');
}
});
原创Facebook指南: http://developers.facebook.com/docs/reference/javascript/FB.login
答案 1 :(得分:1)
我做了这个解决方案来检查“user_friends”和“publish_actions”权限,如果不允许这两者,则强制用户“重新进行autenticate”。只有在给出所有权限时才会调用回调函数。
function login(cb,forceAuth) {
FB.getLoginStatus(function (response) {
if (response.status !== 'connected' || forceAuth==true){
FB.login(function (response) {
checkPermissions(cb,false);
}, {scope: 'publish_actions,user_friends'});
} else {
checkPermissions(cb);
}
});
}
function checkPermissions(cb,forceAuth){
FB.api(
"/me/permissions",
function (response) {
if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 ||
response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) {
if (forceAuth!=false)
login(cb,true);
} else {
cb();
}
}
);
}
使用方法:
login(function () {
myLogedAndAllowedFunction();
});