构建您自己的 Facebook-Login-Button 并使用以下代码实现登录功能:
<div class="fb-login-button" data-scope="email" onclick="fbStatus();">Connect</div>
<script>
function fbInit() {
window.fbAsyncInit = function () {
FB.init({
appId: 'XXXXX',
status: false,
cookie: true,
xfbml: true
});
//auth.statusChange
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status.indexOf('connected') != -1) {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
//Handle the access token
});
} else if (response.status.indexOf('not_authorized') != -1) {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
};
(function (d) {
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.js";
ref.parentNode.insertBefore(js, ref);
} (document));
}
function fbStatus() {
//auth.statusChange
FB.Event.subscribe('auth.authResponseChange', function (response) {
if (response.status.indexOf('connected') != -1) {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
//Handle the access token
});
} else if (response.status.indexOf('not_authorized') != -1) {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
//Handle the access token
});
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}
</script>