Facebook JS SDK:登录前检查权限弹出窗口
我想在显示iframe登录请求扩展perms之前检查登录用户的权限,但是iframe弹出窗口被浏览器ff阻止,chrome测试。我想避免这种情况,我很确定这是因为js函数的结果是“嵌套的”——我的js聪明是有限的,所以请原谅 我猜想,如果我可以在不传递结果的情况下将所有内容保留在原始函数中,浏览器仍会将登录iframe视为“用户启动”而不是阻塞 但我无法做到这一点-例如,为什么下面的结果是data=undefinedFacebook JS SDK:登录前检查权限弹出窗口,facebook,facebook-iframe,facebook-javascript-sdk,Facebook,Facebook Iframe,Facebook Javascript Sdk,我想在显示iframe登录请求扩展perms之前检查登录用户的权限,但是iframe弹出窗口被浏览器ff阻止,chrome测试。我想避免这种情况,我很确定这是因为js函数的结果是“嵌套的”——我的js聪明是有限的,所以请原谅 我猜想,如果我可以在不传递结果的情况下将所有内容保留在原始函数中,浏览器仍会将登录iframe视为“用户启动”而不是阻塞 但我无法做到这一点-例如,为什么下面的结果是data=undefined var data = FB.api( { method: 'fql.query
var data = FB.api(
{
method: 'fql.query',
query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid
});
alert(data); // = undefined
我目前的全部代码是:
<button id="myButton" >Test Publish Event</button>
<script>
$('#myButton').bind('click', function() {
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
FB.api(
{
method: 'fql.query',
query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid
},
function(response) {
if(response[0].create_event == 1) {
alert('permission granted');
} else {
alert('permission absent');
FB.login( function(response) {if (response.session) {
handleSessionResponse(response);
if (response.perms) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
alert(response.perms);
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'create_event,rsvp_event'});
}
}
);
</script>
耶!同样的问题不是同样的问题,但同样的功能困扰了我很多,但经过数小时对各种来源的研究,我终于找到了解决方案 第一 这是错误的!因为所有fb请求都是异步执行的,并且在执行请求后,检索数据值的唯一方法是在该请求内。前
FB.api(
{
method: 'fql.query',
query: 'SELECT create_event FROM permissions WHERE uid=' + response.session.uid
},function(response){
This function will run after the request is finished and ONLY inside here you can read the results data
ex alert(response.data);
});
}
});
第二,由于安全原因,您的权限请求弹出窗口被chrome阻止Firefox允许其他浏览器请求是否允许
仅当从用户操作事件(如.onclick)打开一个弹出窗口时,才允许使用该弹出窗口
因此,您可以将函数附加到允许的.onclick事件
第三,检查用户是否具有使用您的应用程序所需的权限的我的解决方案是:
FB.getLoginStatus(function(response) {
if (response.status.toString().indexOf("connected")>-1) {
initAll();
} else {
requestPermisions proceedure
}
此函数用于检查用户是否已连接并以其他方式授予应用程序权限。返回response.status=unknown
现在
权限弹出框阻止问题有两种解决方案
第一个解决方案=将FB.login函数附加到button.Ex的onclick事件
ifUserNotgrandedPermisions{document.getElementByid("aLogInButton").onclick = function(){
FB.login(....blah blah blah);
};
第二个解决方案,也是我实现的一个,是将iFrame重定向到请求权限页面,而不是弹出
下面是一个完整的解决方案,它检查用户是否登录并授予了perms…如果没有,它会要求用户登录,如果登录则请求权限只需询问permsif有perms只需登录
FB.init({appId: 'YourAPPID', status: true, cookie: true, xfbml: true, oauth : true});
FB.getLoginStatus(function(response) {
if (response.status.toString().indexOf("connected")>-1) {
initAll(); //User is connected and granted perms Good to go!
} else {
//location更改浏览器url,而不是iframe的url
//此url是专门为您的应用程序请求perms而形成的。您可以更改权限和appID
//redirect_uri=将其更改为应用程序画布页面
top.location=window.location="http://www.facebook.com/dialog/oauth/?scope=read_stream,publish_stream,friends_photos,friends_activities&client_id="yourAPPID(nobrackets)"&redirect_uri=http://apps.facebook.com/filtered_feed/&response_type=code";}});};
功能打开弹出窗口登录facebook并进行授权
function attemptLogin(){
FB.login(function(response) {
if (response.authResponse) {
login();
} else {
//if user didn't logged in or didn't authorize your application
}
}, {scope: 'offline_access,publish_stream,manage_pages'}); //YOUR PERMISSION
}
FB.init({
appId: 'YOUR APP ID',
status: true,
cookie: true,
xfbml: true,
oauth : true
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
/*var uid = response.authResponse.userID; //FACEBOOK_ID
var accessToken = response.authResponse.accessToken;*/ //ACCESS TOKEN
FB.Canvas.setAutoResize();
if (response.authResponse) {
// logged in and connected user, someone you know.
// YOUR SUCCESS CODE HERE
}
}else {
attemptLogin();
}
});
function attemptLogin(){
FB.login(function(response) {
if (response.authResponse) {
login();
} else {
//if user didn't logged in or didn't authorize your application
}
}, {scope: 'offline_access,publish_stream,manage_pages'}); //YOUR PERMISSION
}