gpt4 book ai didi

Facebook JS SDK : Check permissions before Login PopUp

转载 作者:行者123 更新时间:2023-11-30 05:27:02 25 4
gpt4 key购买 nike

我想在显示 iframe 登录以请求扩展权限之前检查已登录用户的权限,但 iframe 弹出窗口被浏览器阻止(ff,chrome 测试)。我想避免这种情况,我很确定它是因为 js 函数的结果是“嵌套”的——我的 js 聪明是有限的,所以请原谅。

我猜如果我可以保留原始函数中的所有内容而不传递结果,浏览器仍会将登录 iframe 视为“用户启动”而不是阻止。

但我无法做到这一点 - 例如为什么以下会导致 data = undefined。

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>

最佳答案

耶!同样的问题(好吧,不是同样的问题,而是相同的所需功能)困扰了我很多,但在对各种来源进行了数小时的研究后,我终于找到了解决方案!

第一

var data = FB.api(

这是错误的!因为所有 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 事件。

3rd 我检查用户是否具有使用您的应用程序所需的权限的解决方案是:

 FB.getLoginStatus(function(response) {
if (response.status.toString().indexOf("connected")>-1) {
initAll();
} else {

requestPermisions proceedure
}

此函数检查用户是否已连接并以其他方式向您的应用授予权限 response.status = "unknown"返回。

现在....

权限弹出框问题有2种解决方法。

第一个解决方案 = 将 FB.login() 函数附加到按钮的 onclick 事件。例如。

     ifUserNotgrandedPermisions{document.getElementByid("aLogInButton").onclick = function(){
FB.login(....blah blah blah);
};

第二个解决方案和我实现的一个是将 iFrame 重定向到请求权限页面,而不是弹出

下面是一个完整的解决方案,它检查用户是否已登录并授予权限...如果没有,它会要求用户登录然后请求权限(如果已登录,只需询问权限)(如果有权限,则只需登录) )

    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 {

//top.location 更改浏览器 url 而不是 iframe 的 url

//这个 url 是专门为你的应用请求 perms 的。您更改权限和您的 appID

//redirect_uri = 将此更改为您的应用程序 Canvas 页面

          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 JS SDK : Check permissions before Login PopUp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4048385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com