如何正确使用facebook OAuth2



在拼命尝试将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个小时打孔墙,因为我放错了一些代码。我把错误弄丢了。:谢谢大家。

给你,兄弟!

  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. 一旦您有了用户数据,您可以将其传递给服务器并建立会话。一旦用户点击注销按钮,您应该清除服务器上的会话。

祝一切顺利!:)

最新更新