Facebook第三方登录 使用JavaScript SDK

PHP交流群:294088839
他官网给的php第三方登录坑一大堆,本人实在看不懂.研究了一下他的JS SDK,总结出以下简单方法

// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
    if (response.status === 'connected') {
        //console.log(response);
        // Logged into your app and Facebook.
    } else {
        // The person is not logged into your app or we are unable to tell.
        document.getElementById('status').innerHTML = 'Please log ' +
            'into this app.';
    }
}

window.fbAsyncInit = function() {
    FB.init({
        appId      : 'APPID',
        cookie     : true,  // enable cookies to allow the server to access
                            // the session
        xfbml      : true,  // parse social plugins on this page
        version    : 'v3.2', // The Graph API version to use for the call  版本号很重要
        oauth: true
    });

    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);
    });

};

// Load the SDK asynchronously
(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 = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

// Here we run a very simple test of the Graph API after login is
// successful.  See statusChangeCallback() for when this call is made.


function login(){

        FB.login(function(response) {
            if (response.authResponse) {
                var userId=response.authResponse.userID;
                FB.api('/me', function(response) {
                    $.post('{:url(\'Login/facebookcallback\')}',{userid:userId},function(res){
                          if(res.code===1){
                              layer.msg(res.msg,{icon:1,time:1000},function(){
                                  location.href=res.url;
                              })
                          }else{
                              layer.msg(res.msg)
                          }
                    });

                });
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }

    })

}

你可能感兴趣的:(JS)