我有两个PHP文件用于创建登录链接并接收访问令牌(回调)。我的问题是,我无法接收或获取访问令牌的到期日期。
这是我创建链接的脚本:
<?php
session_start();
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook/');
require_once __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook( array('app_id' => 'XX', 'app_secret' => 'XX', 'default_graph_version' => 'v2.5'));
$permissions = ['email', 'user_likes', 'user_birthday', 'manage_pages ', 'user_photos', 'publish_actions', 'publish_pages' ];
$helper = $fb -> getRedirectLoginHelper();
$loginUrl = $helper -> getLoginUrl('blabla/facebook-login-callback.php', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>'; ?>
这很好用,我的回调文件被调用。
<?php
session_start();
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook/');
require __DIR__ . '/autoload.php';
$fb = new Facebook\Facebook(['app_id' => 'XX', 'app_secret' => 'XX', 'default_graph_version' => 'v2.5']);
$helper = $fb -> getRedirectLoginHelper();
try {
try {
$accessToken = $helper -> getAccessToken();
$isexpired = $accessToken -> isExpired();
$expiresAt = $accessToken -> getExpiresAt();
echo "AccessToken: ";
echo $accessToken -> getValue();
echo "<br/>ExpiresAt: ";
echo $expiresAt;
echo "<br/>IsExpired: ";
echo $isexpired;
die();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e -> getMessage();
exit ;
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e -> getMessage();
exit ;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e -> getMessage();
exit ;
} ?>
但现在我的问题是,我没有得到失效日期以及'isExpired'值,它只是空的。我唯一得到的是访问令牌作为字符串(通过'getValue()')。
有人能帮助我吗?我认为我的代码应该是正确的?
答案 0 :(得分:0)
<强>代码强>:
{
$accessToken =$helper->getAccessToken();
$isexpired = $accessToken -> isExpired();
$expiry_status=json_encode($isexpired);
$expiresAt = $accessToken -> getExpiresAt();
$mydate=json_encode($expiresAt);
echo "AccessToken: ";
echo $accessToken -> getValue();
echo "<br/>ExpiresAt: ";
echo $expiresAt->date;
echo "<br/>IsExpired: ";
echo $expiry_status;
die();
}