0

当我运行此代码时,会弹出登录名,并且我的控制台日志开始无限循环运行。我不确定我是否正确使用了 FB.login() 方法。在用户到达我的应用程序连接的情况下,我想提示权限,如果他们被授予,是否会返回一个“连接”状态,如果它被拒绝,我应该检查和处理?

 FB.Event.subscribe('auth.authResponseChange', function(response) {

    if (response.status === 'connected') {

//if connected open permissions dialog

       FB.login(function(response) {
//if user grants permissions, make ajax call to pass signed request to php page

       if(response.status === 'connected'){


               $.ajax({
        url : "http://xxxxxxio/bn/s_Request.php",
        type : 'POST',
        data: {signed_request: response.authResponse.signedRequest},
        success : function (result) {

           console.log(result);
        },
        error : function () {
           alert("error parsing signed request");
        }
      });//closes ajax 

            }else{
                //if authorization cancelled, redirect to home page

                     window.location = "http://spilot.kd.io/bn/index.php";

                }//closes else

 }, {scope: 'user_location,user_likes'});

      testAPI();

    } else if (response.status === 'not_authorized') {
      // the person is logged into Facebook, but not into the app

     FB.login(function(response) {
             // handle the response
 }, {scope: 'user_location,user_likes'});

    } else {
      //  the person is not logged into Facebook, call the login() 

      FB.login(function(response) {
             // handle the response
 }, {scope: 'user_location,user_likes'});

    }//closes else

  });//closes event subscribe

var testAPI = function() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Good to see you, ' + response.name + '.');
    });
  };
4

1 回答 1

1

如果用户“已连接”到达您的应用程序,则他/她已经通过了您的应用程序授权并提供了必要的权限。请参阅此页面https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

FB.getLoginStatus 允许您确定用户是否登录到 Facebook 并验证了您的应用程序。用户有三种可能的状态:

  • 用户已登录 Facebook 并已验证您的应用程序(已连接
  • 用户已登录 Facebook,但尚未对您的应用程序进行身份验证(not_authorized
  • 用户此时未登录 Facebook,因此我们不知道他们是否已验证您的应用程序(未知

CBroe 是对的,你想监控你从每个调用中得到的响应,FB.Event.subscribe('auth.authResponseChange'返回一个不同的响应 FB.login

如果您想提供“更多”权限,您可以稍后在您的应用程序中发布它们。

为了减少冗余,我们可以去掉这个函数

FB.login(function(response) {
             // handle the response
 }, {scope: 'user_location,user_likes'});

并放入类似的东西

function login() {
    FB.login(function(response) {
                 // handle the response
     }, {scope: 'user_location,user_likes'});
}

因此,您可以拥有条件的最后两部分,例如

} else if (response.status === 'not_authorized') {
  // not_authorized
    login();
} else {
  // not_logged_in
    login();
}

我快速浏览了您之前的问题,看来您已经掌握了窍门,但这是一种非常艰难的迂回曲折的到达那里的方式。因此,为了完全避免这种情况,以便您了解全局,我建议您退后一步,全面审视您的应用程序,并回到您正在尝试做的主要目标

听起来您正在通过 AJAX 向 PHP SDK(?)发送 signed_request,而 PHP SDK 应该已经有一个函数库来处理这个问题。所以,是的,退后一步,以一个浓缩形式的问题回来。

于 2013-07-27T17:39:11.900 回答