【问题标题】:Facebook OAUTH2 and PHP/JSDKFacebook OAUTH2 和 PHP/JSDK
【发布时间】:2011-10-02 15:18:24
【问题描述】:

我已注册 auth.login 以便对我的服务器进行 ajax 回调并更新登录计数。它不起作用,因为 php sdk 坚决拒绝查看用户是否正确登录。

JS代码:

    window.fbAsyncInit = function () {
    var channelurl='http://'+window.location.hostname+'/channel.html';
        FB.init({
            appId : window.appid,
            status: true,
            cookie: true,
            xfbml: true,
            channelURL : channelurl, // channel.html file
            oauth  : true // enable OAuth 2.0
        });
        FB.Event.subscribe('auth.login', function (response) {

                   $("#fbconnecttext").html("<a>Logging in...</v>");

                   $.ajax({
                      url: "fbupdatelogincount",
                      type: "GET",
                      cache: false,
                      success: function (html) {
                          window.setTimeout('$("#fbconnecttext").html("")', 10000);
                          var rec = JSON.parse(html);
                          window.numlogins = rec["numlogins"];
                          FB.api('/me', function (response) {
                             if (window.numlogins > 1) {
                                 $("#fbconnecttext").html(window.welcomebacktext.replace("%s", response.first_name));
                                 $("#fbadminimg").attr("src", "common-images/smiley");
                             }
                             else {
                                 alert(window.firstlogintext.replace("%s", response.first_name));
                             }

                             });

                      }
                      });
               });

        FB.Event.subscribe('auth.logout', function (response) {
                   $("#fbconnecttext").html(window.fbconnecttext);
                   $("#fb-like").show();
                   FB.XFBML.parse($('#fb-like').is());
               });
        FB.Event.subscribe('auth.sessionChange', function (response) {});
    };

(function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "http://connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
}

php页面

require_once("utils.php");
logText("In  fbUpdatelogincount");
if(fbAuthentication()){
    logText("authenticated in  fbUpdatelogincount");
    $r=fbUpdateStats();
    if(isset($_REQUEST["field"]))
        echo $r[$_REQUEST["field"]];
    else
        echo json_encode($r);
}
echo "";

最后是 fbAutentication 代码:

function fbAuthentication(){

    global $facebook;
    $facebook = new Facebook(array(
                     'appId' => getSetting("fb:app_id"),
                     'secret' => getSetting("fb:secret")
                     ));
    if($facebook->getUser()){
        try {
            global $fb_isadmin,$fb_me;
            $fb_me = $facebook->api('/me');
            $fb_isadmin=strstr(getSetting("fb:admins"),$facebook->getUser())!=false;
            fbUpdateStats();
            return true;
        } catch (FacebookApiException $e) {
            /* exception handling todo */
        }
        return true;
    }else logText("No Valid user");

    return false;

}

主要问题是 ajax 调用触发了 url fbupdatelogincount 但 PHP 端说“不,没有人登录”。有任何想法吗?相同的设置在 3.1.1 之前运行良好

【问题讨论】:

    标签: facebook facebook-javascript-sdk facebook-php-sdk


    【解决方案1】:

    这似乎没有记录在任何地方,但似乎将应用程序机密传递给 auth.login 事件会导致它成功触发。

    试试这个:

    FB.Event.subscribe('auth.login', function(response) {
        // callback
    }, { secret:'<?php print $facebook->getApiSecret(); ?>' });
    

    Facebook 已经修复了最初的问题。

    【讨论】:

    • 几个问题:这种未记录的行为究竟修复了什么?其他人如何使用此修复程序?这不会造成巨大的安全漏洞吗?你最后不会公开你的 FB AppId 和 FB AppSecret 吗?
    • Facebook 应用程序设置下有一个选项,在标记为“需要 auth.login 的应用程序密码”的高级选项卡下,这是我首先想到尝试这个的地方。当时,这是唯一 修复会导致 auth.login 事件触发,以便可以正确加载来自 Facebook 的用户会话。此时,该选项似乎已从应用程序设置中删除,原始问题已被 Facebook 修复。这就是使用 API 的本质,这些 API 不断变化,而开发人员没有太多通知。
    • 哇。我在 OAuth 2.0 和 Facebook 上玩得很开心。强制 auth.login 触发将是一个很好的解决方法。如果弹出窗口出现但您已经登录,则它现在的工作方式,不会触发任何事件。嗯,我知道。
    【解决方案2】:

    我终于找到了另一个解决方案:在我的 ajax 回调中,我将 accesstoken 添加为 GET 参数,在 php url 处对其进行解码,然后调用 $facebook->setAccessToken($at)。现在工作正常。因此,这是新 SDK 协同工作中的一个错误。多么美好的一天... ;) –

    FB.Event.subscribe('auth.authResponseChange', function (response) {
        FB.XFBML.parse(document.getElementById('fb-like'));
        if (response.authResponse) {
        FB.api('/me', function(response) {
               // $("#fbconnecttext").html('<a class="fbUserName">'+response.first_name+'</a>');
               });
            $.ajax({
                url: "fbupdatelogincount?accesstoken="+response.authResponse.accessToken,
                type: "GET",
                success: function (html) {
                    if (html) {
                var rec = JSON.parse(html);
                        window.numlogins = rec["numlogins"];
                        FB.api('/me', function (response) {
                            if (window.numlogins > 1) {
                                $("#fbconnecttext").html(window.welcomebacktext.replace("%s", response.first_name));
                                $("#fbadminimg").attr("src", "common-images/smiley");
                            }
                            else {
                                alert(window.firstlogintext.replace("%s", response.first_name));
                            }
    
                        });
                    }
    
                }
            });
        }else{
        //logged out
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2012-01-27
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2023-03-15
      • 2013-10-18
      相关资源
      最近更新 更多