我已经使用API将Facebook登录信息集成到PHP中。它已成功重定向到FACEBOOK凭据页面,并返回到我的主页并检索了代码,但是问题是在获取代码后,我试图获取ACCESSTOKEN,并且它返回null。
下面是我的代码:
require 'fb/Facebook/autoload.php';
function login_via_fb($param = array())
{
global $outputjson, $gh, $db;
$outputjson['success'] = 0;
$fb = new Facebook\Facebook([
'app_id' => FB_APP_ID,
'app_secret' => FB_APP_SECRET,
'default_graph_version' => 'v3.2',
]);
$helper = $fb->getRedirectLoginHelper();
if(isset($_GET['state'])){
$_SESSION['FBRLH_state']=$_GET['state'];
}
$fb_code = $gh->read('code');
$logout = $gh->read('logout');
$permissions = ['email']; // optional
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken(WEB_PANEL_PATH);
}
$outputjson['test_Access_token'] = $accessToken;
} catch (Facebook\Exceptions\facebookResponseException $e) {
$outputjson['response_error'] = 'Graph returned an error: ' . $e->getMessage();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$outputjson['sdk_Error'] = 'Facebook SDK returned an error: ' . $e->getMessage();
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
$outputjson['facebook_access_token'] = (string)$accessToken;
$oAuth2Client = $fb->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$outputjson['facebook_access_token'] = (string) $longLivedAccessToken;
$fb->setDefaultAccessToken($accessToken);
$_SESSION['facebook_access_token'] = $outputjson['facebook_access_token'];
}
if (!empty($fb_code)) {
try {
$profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
$requestPicture = $fb->get('/me/picture?redirect=false');
$picture = $requestPicture->getGraphUser();
$profile = $profile_request->getGraphUser();
$fbid = $profile->getProperty('id');
$first_name = $profile->getProperty('first_name');
$last_name = $profile->getProperty('last_name');
$user_name = $profile->getProperty('name');
$email = $profile->getProperty('email');
$fbpic = "<img src='" . $picture['url'] . "' class='img-rounded'/>";
$user_exist = $db->select('user_id','tbl_users',' fb_id="' . $fbid . '"');
$data['username'] = $user_name;
$data['first_name'] = $first_name;
$data['last_name'] = $last_name;
$data['email'] = $email;
$data['profile_pic'] = $picture['url'];
$data['user_type'] = 'facebook';
$data['date_added'] = date("Y-m-d H:i:s");
if(!empty($user_exist)){
$data['date_modified'] = date("Y-m-d H:i:s");
$db->update('tbl_users',$data, ' fb_id = "' . $fbid . '"');
$data['user_id'] = $user_exist[0]['user_id'];
}else{
$data['date_modified'] = date("Y-m-d H:i:s");
$data['fb_id'] = $fbid;
$user_id = $db->insert('tbl_users',$data);
$data['user_id'] = $user_id;
}
$outputjson['user_data'] = $data;
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$outputjson['response_error'] = 'Graph returned an error: ' . $e->getMessage();
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$outputjson['sdk_Error'] = 'Facebook SDK returned an error: ' . $e->getMessage();
}
}
} else {
$loginUrl = $helper->getLoginUrl(WEB_PANEL_PATH, $permissions);
$outputjson['login_url'] = $loginUrl;
$outputjson['error'] = $helper->getError();
}
$outputjson['success'] = '1';
}
如何修复accessToken? 请帮忙............