Facebook JS SDK:在登录PopUp之前检查权限

时间:2010-10-29 01:36:59

标签: facebook facebook-iframe facebook-javascript-sdk

我希望在呈现iframe登录以请求扩展权限之前检查已登录用户的权限,但iframe弹出窗口被浏览器阻止(ff,chrome tests)。我想避免这种情况,我很确定它因为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>

2 个答案:

答案 0 :(得分:8)

YAY!同样的问题(没有相同的问题,但相同的所需功能)困扰我很多,但最终在各种来源的研究HOURS后我找到了解决方案!!

第一

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允许其他浏览器允许或不允许)

&#34;只有从用户操作事件(例如.onclick&#34;)打开一个弹出窗口时,才允许弹出一个弹出窗口。 因此,您可以将该函数附加到允许的.onclick事件。

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

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

requestPermisions proceedure
 }

此功能检查用户是否已连接并以其他方式为您的应用授予权限response.status =&#34; unknown&#34;归还。

现在...

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

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

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

//此网址专门用于为您的应用询问权限。您更改了权限和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";}});};

答案 1 :(得分:1)

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

打开弹出窗口登录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
}