Request had insufficient authentication scopes [403]

1.1k views Asked by At

I'm trying to use the Google Search Console codes to get position about my website in a Google Search. I using the php-library by Google, so I created a function:

function getPosicao(){


        /* Using Stash for caching */
        //$objCache = new Pool( new FileSystem );

        //$objCacheLogger = new Logger;
        //$hdTokenCallback = function( $cacheKey, $accessToken ) use ( $objCacheLogger ) {
        //    $objCacheLogger->debug( sprintf( 'A new access token \'%s\' has beeen received for the cache key %s', $accessToken, $cacheKey ) );
        //};

        /* Creating the client */
        $objGoogleApiClient = new Google_Client();
        putenv('GOOGLE_APPLICATION_CREDENTIALS=xxxxxxxxxx.json');
        $objGoogleApiClient->useApplicationDefaultCredentials();
        $objGoogleApiClient->setApplicationName('getPosition');
        $objGoogleApiClient->setAccessType('offline');
        $objGoogleApiClient->setScopes( ['https://www.googleapis.com/auth/webmasters.readonly']);
        //$objGoogleApiClient->setCache( $objCache );
        //$objGoogleApiClient->setTokenCallback( $hdTokenCallback );

        /* Refresh token when expired */
        if( $objGoogleApiClient->isAccessTokenExpired() ){
            $objGoogleApiClient->refreshTokenWithAssertion();
        }

        /* Creating the actual service */

        $query = new Google_Service_Datastore_Query([
            'kind' => [
                [
                    'name' => 'Book',
                ],
            ],
            'order' => [
                'property' => [
                    'name' => 'title',
                ],
                'direction' => 'descending',
            ],
            'limit' => 10,
        ]);

        $datastore = new Google_Service_Datastore($objGoogleApiClient);
        // build the request and response
        $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]);
        $response = $datastore->projects->runQuery('XxxxxxxXxXxxxx', $request); 


       return $response;

    }

And this code returns that error:

Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "errors": [ { "message": "Request had insufficient authentication scopes.", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } in /var/www/html/contabilidade/post-manager/wp-content/themes/monitor/functions/backend/vendor/google/apiclient/src/Google/Http/REST.php on line 118

I followed Google's instructions, but this error still returns. Any suggestion? Thanks in advance!

0

There are 0 answers