Answer the question
In order to leave comments, you need to log in
How to fix "An active access token must be used to query information about the current user" error?
Hello.
--
I made the code according to the documentation https://developers.facebook.com/docs/php/howto/exa...
, (only in Yii), everything works, but when the last step for getting user data is approaching, an error pops up:
An active access token must be used to query information about the current user
<?php
namespace ...\...\models;
use Yii;
use Facebook\Facebook;
class FaceBookModel
{
public $appId; // Yii::$app->facebook->appId
public $appSecret; // Yii::$app->facebook->appSecret
public $defaultGraphVersion; // Yii::$app->facebook->defaultGraphVersion
public $urlRedirect; // Yii::$app->facebook->urlRedirect
public function fb()
{
return $fb = new Facebook([
'app_id' => Yii::$app->facebook->appId,
'app_secret' => Yii::$app->facebook->appSecret,
'default_graph_version' => Yii::$app->facebook->defaultGraphVersion,
]);
}
public function getTokenFB()
{
$helper = self::fb()->getRedirectLoginHelper();
$permissions = ['email','public_profile']; // Optional permissions
$loginUrl = $helper->getLoginUrl(Yii::$app->facebook->urlRedirect, $permissions);
return $loginUrl;
}
// state
public function getState()
{
$parts = parse_url(self::getTokenFB());
parse_str($parts['query'], $query);
session_start();
$_SESSION['fb_access_token'] = $query['state'];
return $_SESSION['fb_access_token'];
}
public function getUser()
{
$_SESSION['fb_access_token'] = (string) $accessToken;
$accessToken = Yii::$app->facebook->appId.'|'.Yii::$app->facebook->appSecret;
try {
$response = self::fb()->get(
'/me?fields=id,name,email,first_name,last_name,picture,gender', $accessToken
);
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
var_dump($graphNode);
}
}
$helper = $fb->getRedirectLoginHelper();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question