Facebook JS SDK:在登录PopUp之前检查权限 [英] Facebook JS SDK: Check permissions before Login PopUp

查看:108
本文介绍了Facebook JS SDK:在登录PopUp之前检查权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查登录用户的权限,然后再提交iframe登录名才能请求扩展perms,但是iframe弹出窗口被浏览器阻止(ff,chrome测试)。我想避免这个,我很确定它,因为js函数的结果是嵌套的 - 我的js聪明是有限的,所以请原谅。

I want to check permissions of a logged in user before presenting the iframe login to request extended perms, but the iframe popup gets blocked by the browser (ff, chrome tested). I want to avoid this and i'm pretty certain its because the results of the js functions are 'nested' - my js cleverness is limited so excuse me.

我猜测,如果我能保持原来的功能,而不会传递结果,浏览器仍然会看到登录iframe为用户启动,而不是阻止。

I'm guessing that if I can keep everything in the original function without passing down the results the browsers would still see the login iframe as 'user initiated' and not block.

但是我无法做到这一点 - 例如为什么以下结果data = undefined。

But i'm unable to do this - e.g. why does the following result in 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>


推荐答案

YAY!同样的问题问题(没有相同的问题,但是相同的期望的功能)困扰了我很多,但最后在研究的时间之后,我想出了一个解决方案!!

YAY ! The same problem problem(well no the same problem but the same desired functionality) troubled me a lot but finally after HOURS of Research on various sources i figured out a solution!!

1

var data = FB.api(

这是错误的!因为所有的fb请求都是异步执行的,而且在执行该请求之后,唯一的方法是检索数据值。

this is wrong! Because all fb requests are executed asynchronously and the only way to retrieve the data values are inside that request after its been executed. ex

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);
});         
  }
});

第2您的许可请求弹出窗口被Chrome屏蔽,出于安全原因(firefox允许其他浏览器要求允许或不允许)

2nd Your Permission request pop up is blocked by chrome for security reasons(firefox allows it other browsers ask to allow or not)

只有在用户操作事件(如.onclick
)打开的情况下,才能允许弹出窗口。对于允许的.onclick事件的功能。

"One pop-up can only be allowed if its opened from a user-action event such as .onclick" So you can attach the function to an .onclick event to be allowed.

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

3rd My solution for checking if a user has the required permissions to use your app is :

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

requestPermisions proceedure
 }

此功能检查用户是否已连接,并已向您的应用程序授予其他方式的权限response.status =未知。

This function checks if a user is connected and has given permissions to your app other way response.status = "unknown" is returned.

现在....

权限弹出窗口问题有2个解决方案。

The permission pop-up block problem has 2 solutions.

第一个解决方案=将FB.login()函数附加到一个button.Ex的onclick事件

1st solution = attach the FB.login() function to an onclick event of a button.Ex.

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

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

2nd solution and the one i implemented is redirecting the iFrame, to a request permission page, and not pop up

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

Below is a full solution, that checks if user is logged in and granted perms...if not it asks the user to log in and then requests permissions (if logged in just ask perms)(if has perms just logs in)

    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更改浏览器网址,而不是iframe的url

//top.location changes the browsers url and NOT the iframe's url

//这个url是特别形成的,要求你的应用程序,你更改权限和你的appID

//This url is specially formed to ask perms for your app. You change the permissions and your appID

// redirect_uri =将其更改为您的应用程序画布页面

//redirect_uri = Change this to your application canvas page

          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:在登录PopUp之前检查权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆