Facebook OAUTH2和PHP / JSDK

时间:2011-10-02 15:18:25

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

我已经使用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

之前,相同的设置工作正常

2 个答案:

答案 0 :(得分:1)

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

试试这个:

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

<击>

此问题已由Facebook修复。

答案 1 :(得分:0)

我终于找到了另一种解决方案:在我的ajax回调中,我将accesstoken添加为GET参数,在php url处解码,然后调用$ facebook-&gt; 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
    }
});