我是Facebook Graph API的新開發者。我遵循本教程:https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0。我有一次測試,它的工作原理。我有修改和重新測試,它的工作。但是,第二天,什麼都沒有......我的Facebook會話令牌有變化,我不知道爲什麼和如何。PHP /圖形API:爲什麼我的Facebook會話不起作用?
有我的代碼:
// ALL includes and variables
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication($app_ID, $app_secret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper($url);
// generate login url with scope, each permission as element in array
$loginUrl = $helper->getLoginUrl(array($access_right));
// see if a existing session exists
if (isset($_SESSION) && isset($_SESSION['fb_token'])) {
// create new session from saved access_token
$session = new FacebookSession($_SESSION['fb_token']);
// validate the access_token to make sure it's still valid
try {
if (!$session->validate()) {
$session = null;
}
} catch (Exception $e) {
// catch any exceptions
$session = null;
}
}
if (!isset($session) || $session === null) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
// handle this better in production code
print_r($ex);
} catch(Exception $ex) {
// When validation fails or other local issues
// handle this better in production code
print_r($ex);
}
}
// see if we have a session
if (isset($session)) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession($session->getToken());
// graph api request for user data
$graphObject = (new FacebookRequest($session, 'GET', '/me/accounts'))->execute()->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r($graphObject, 1) . '</pre>';
$page_token = $graphObject['data'][0]->access_token;
echo '<p>Page token : '.$page_token.'</p>';
$session = new FacebookSession($page_token);
/* make the API call */
$request = new FacebookRequest(
$session,
'POST',
'/'.$pageID.'/feed',
array (
'message' => 'This is a test message',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
// print profile data
echo '<pre>' . print_r($graphObject, 1) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo '<a href="' . $helper->getLogoutUrl($session, 'http://yourwebsite.com/app/logout.php') . '">Logout</a>';
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl(array('email', 'user_friends')) . '">Login</a>';
}
我的目標就是把一個消息我的粉絲專頁上,和它的作品第一次。爲什麼變量「$ _SESSION ['fb_token']」現在是NULL?我如何才能使它工作?
感謝您的回覆!
編輯1: 我發現我的會話已將CSRF標題:'fb_token'更改爲'FBRLH_state'。關於這種變化的想法?
我有這個相同的問題! – Jeshii
我有同樣的問題,現在我仍然搜索如何解決這個問題。 – viyancs
Facebook的會話總是使用'FBRLH_state',問題在於FBRLH_state改變的原因? – ilumin