如何正确使用facebook OAuth2?

时间:2013-03-16 13:29:01

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

经过3天的拼命尝试将facebook登录系统整合到我的网站后,我得出结论,我悲惨地失败了。我只是用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谢谢你们。

1 个答案:

答案 0 :(得分:2)

这里你去兄弟!

  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. 获得用户数据后,可以将其传递给服务器并设置会话。用户单击注销按钮后,您应该清除服务器上的会话。

  7. 一切都好! :)