I have a Facebook canvas app.
I need an access token, the user authorizes the app and the apps main page works fine, but not the other pages. When I click one link in my app that redirects to subpage, it need authorization again.
my controller code (Yii) is:
protected function beforeAction($action)
{
session_start();
$this->fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.2',
]);
$this->checkFbLogin();
$this->getFbUser();
if (!Yii::app()->player->uid) {
$this->registerFbUser(); //connect facebook uid with game uid
}
return true;
}
private function checkFbLogin()
{
$helper = $this->fb->getCanvasHelper();
try {
$accessToken = $helper->getAccessToken();
} 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;
}
if (! isset($accessToken)) {
//echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
echo '<script>top.window.location="' . $this->getLoginUrl() . '"; </script>';
exit;
}
$this->accessToken = $accessToken;
}
private function getLoginUrl()
{
$helper = $this->fb->getRedirectLoginHelper();
$permissions = []; // optional
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/my-game/', $permissions);
return $loginUrl;
}
I can't see what is the problem. I can't find anything that can help.
When I click a link in the app, I don't get my accessToken in the checkFbLogin()
function.
I am using the PHP SDK v5.0.0 installed with composer.