考虑以下来自 developer.facebook.com 的代码片段
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;
} 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.
}
});
接下来,考虑以下
$facebook = new Facebook($config);
$user = $facebook->getUser();
$userdata = null;
if($user)
{
try{
$userdata = $facebook->api('/me');
}catch(FacebookApiException $e){
$e->getMessage();
}
}
$loginUrl = $facebook->getLoginUrl(array('scope' => $scope);
if(!$userdata)
{
//redirect to login
}
我的第二个代码正在做第一个代码承诺做的所有事情。那么我为什么要使用第一种方法呢?