我尝试使用Google Search Console代码在Google搜索中获取有关我网站的排名。我使用谷歌的php-library,所以我创建了一个函数:
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;
}
此代码返回该错误:
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
我按照Google的说明操作,但此错误仍然存在。有什么建议吗?提前谢谢!