Facebook OAUTH2 和 PHP/JSDK

发布于 2024-12-07 12:40:49 字数 3427 浏览 0 评论 0原文

我已经注册了 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 之前,相同的设置运行良好

I have registered with auth.login in order to do an ajax call back to my server and update login counts. it doesnt work as the php sdk is resolutely refusing to see that the user is properly logged in.

JS code:

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

The php page

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 "";

And finally the fbAutentication code:

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;

}

The main issue is the ajax call firing up the url fbupdatelogincount but the PHP side saying "nope, no one is logged in". Any ideas? Same setup worked fine prior to 3.1.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2024-12-14 12:40:49

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

试试这个:

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

原始问题此后 Facebook 已修复该问题。

This doesn't seem to be documented anywhere, but it seems that passing the application secret to the auth.login event causes it to fire successfully.

Try this:

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

The original issue has since been fixed by Facebook.

〃温暖了心ぐ 2024-12-14 12:40:49

我终于找到了另一个解决方案:在我的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
    }
});

I finally found another solution : in my ajax call back I added the accesstoken as a GET parameter, decoded it at the php url and then called $facebook->setAccessToken($at). Works fine now. So it IS a bug in the new SDKs working together. What a day... ;) –

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
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文