此代码适用于 Facebook 桌面版本,但不适用于我想获取当前 User Id 的移动设备。
<div id="fb-root"></div>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"
type="text/javascript"></script>
<script>
// Load the SDK Asynchronously
(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 = document.location.protocol + '//connect.facebook.net/en_US/all.js';
ref.parentNode.insertBefore(js, ref);
} (document));
// Init the SDK upon load
var called = false;
window.fbAsyncInit = function () {
FB.init({
appId: 'XXXX', // App ID
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
alert("12");
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
alert("Your UID is " + response.authResponse.userID);
}
else if (response.status === 'not_authorized') {
// not_authorized
alert("3");
login();
alert("5");
} else {
// not_logged_in
alert("4");
login();
}
});
};
if (!called) {
alert("Not Called");
window.fbAsyncInit();
alert(" Called");
}
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Good to see you, ' + response.name + '.');
});
}
function login() {
FB.login(function (response) {
if (response.authResponse) {
// connected
testAPI();
} else {
// cancelled
}
});
}
</script>