I
I
Igor Vasiliev2019-10-16 17:40:00
Facebook
Igor Vasiliev, 2019-10-16 17:40:00

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

My code:
<?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);
        
    }

}

It's clear that "state" interferes with life - I configured that when clicked it is written to the session, and from there it is then inserted into the processing link, the token, as in principle everywhere, is encrypted from two keys, client and secret, so you can display the glued key by default.
$helper = $fb->getRedirectLoginHelper();
It doesn't allow getting a token, maybe it's a bug, when I output an array, its values ​​are empty, I read on the Facebook developers website, they admit that the documentation is outdated, and only applies to version v2.0
But I have version v4.0- if you do strictly according to what is written, then nothing works at all. The feeling that state is created twice, although it is the same in the console and in the address bar. For you to understand, I'm talking about the state that comes from facebook when the redirect occurs. All this dance with tambourines is only to check the correspondence between the sender site and the recipient site - if they do not match, the token is not correct and the authorization does not pass validation. I tested the data through Facebook Developer, the data comes there, but I also get the marker there on click, how it is drawn there is not clear.
Question: how to defeat this bug, who found the solution?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question