【问题标题】:How to properly use facebook OAuth2?如何正确使用 facebook OAuth2?
【发布时间】:2013-03-05 04:44:54
【问题描述】:

在拼命尝试将 facebook 登录系统集成到我的网站 3 天之后,我得出的结论是我惨败。我只是用 JavaScript SDK API 调用创建了一堆乱七八糟的 PHP SDK,结果和预期的一样,没有工作。

我在 Google 和 stackoverflow 上搜索了其他人的问题、教程、解释的答案,但我现在承认自己失败了。

你能给我这个工作的好教程吗?或者你能给我深入了解一下他们的图书馆吗?

希望这不会被关闭。

编辑:这是使用 Facebook 登录系统的推荐方法吗?

编辑 2:

FB.init({ appId : '<?php echo $core->appId; ?>', // App ID from the App Dashboard channelUrl : '//romeo.no-ip.org/94seconds/channel.html', // Channel File for x-domain communication status : true, // check the login status upon init? cookie : true, // set sessions cookies to allow your server to access the session? xfbml : true // parse XFBML tags on this page? });

此代码生成FB.getLoginStatus() called before calling FB.init().

我的页面如下所示:

<!DOCTYPE html>
<html>
    <head>
        <script src="../node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
    </head>
    <body>
        <div id="fb-root"></div>
        <script>
          window.fbAsyncInit = function() {
                FB.init({
                  appId      : '<?php echo $core->appId; ?>', // App ID from the App Dashboard
                  channelUrl : '//romeo.no-ip.org/94seconds/channel.html', // Channel File for x-domain communication
                  status     : true, // check the login status upon init?
                  cookie     : true, // set sessions cookies to allow your server to access the session?
                  xfbml      : true  // parse XFBML tags on this page?
                });
          };

          // Load the SDK's source Asynchronously
          // Note that the debug version is being actively developed and might 
          // contain some type checks that are overly strict. 
          // Please report such bugs using the bugs tool.
          (function(d, debug){
             var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
             if (d.getElementById(id)) {return;}
             js = d.createElement('script'); js.id = id; js.async = true;
             js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js";
             ref.parentNode.insertBefore(js, ref);
           }(document, /*debug*/ false));
        </script>
    </body>
</html>

编辑 3:没关系,我刚刚意识到我已经花了 3 - 4 个小时打墙,因为我放错了一些代码。我已经失去了错误。 :D 谢谢你们。

【问题讨论】:

  • Can you give me a good tutorial for this job? 直接要求人们为您使用 Google。
  • @tradyblix:不。只是询问是否有人遇到过、制作过或学习过。
  • @hakre :你是在温和地建议我不要使用 OAuth2 吗?
  • 不,只是它有一些粗糙的边缘。

标签: php javascript facebook oauth-2.0 facebook-oauth


【解决方案1】:

兄弟,给你!

  1. 这会将 fb js sdk 添加到您的页面

    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=" + APP_ID;
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    
  2. 这将通知您用户是否已登录或他当前的状态

    window.fbAsyncInit = function() {
    
        // Additional init code here
        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {
                // logged in and connected
    
            } else if (response.status === 'not_authorized') {
            // not_authorized - the user has not authorized ur app
    
            } else {
            // User is not_logged_in
    
            }
        });
    
        FB.Event.subscribe('edge.create',
            function(response) {
                //Not relevant here
                //User has just liked your page
                //likeHandler(response);
            });
    };
    
  3. 这是为了让用户登录

    function Login() {
        FB.login(function (e) {
            //        console.log(e.authResponse);
            if (e.authResponse) {
                M_access_token = e.authResponse.accessToken;
    
                //custom function to get user details
                get_details();
            } else {
            //            console.log("Error : Failed to Authorize")
            }
        }, {
            scope: "email,user_likes,publish_stream"
        })
    }
    
    //Notice the scope param above
    
  4. 这是为了注销用户

    function Logout() {
        FB.logout(function(response) {
            // user is now logged out
            });
    }
    
  5. 自定义函数Get_details

    function get_details(typeCalled) {
        FB.api("/me?fields=name,id,email,gender,username,picture", function (e) {
            fbUserObject = e;
            //        M_user_id = e.id;
            //        M_user_full_name = e.name;
            //        M_user_email = e.email;
            //        M_user_gender = e.gender;
            //        M_username = e.username;
            //picture = e.picture.data.url;
        });
    }
    
  6. 获得用户数据后,您可以将其传递给服务器并设置会话。一旦用户单击注销按钮,您应该清除服务器上的会话。

一切顺利! :)

【讨论】:

  • 我不断收到双重FB.getLoginStatus() called before calling FB.init(). all.js (line 52) FB.getLoginStatus() called before calling FB.init(). 错误。
  • 步骤1中如果不提供APP_ID,则无法自动初始化,出现上述错误。否则,您必须通过调用 // init the FB JS SDK FB.init( parameters here) 来手动初始化它; developers.facebook.com/docs/reference/javascript
  • 老兄,你真是个天才。我设法让它工作,这一切都是因为你。谢谢!
  • 我为之而活的这些时刻!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2016-01-18
  • 2016-08-27
  • 2021-10-26
  • 2012-08-25
  • 2014-02-14
  • 2021-05-08
相关资源
最近更新 更多