我有以下错误时,尝试使用谷歌搜索分析API从PHP代码:
PHP致命错误:未捕获异常'Google_Service_Exception'消息'调用POST错误https://www.googleapis.com/webmasters/v3/sites/{MY网站上线/searchAnalytics/query: (403) Insufficient Permission' in/用户/Ihar_Cheliadzinski/Workspace/DSS/Google登录/google-api-client/src/谷歌/Http/REST.php: 110
我已经在API Explorer: https://developers.google.com/apis-explorer/?hl=en_US#p/webmasters/v3/webmasters.searchanalytics.query中测试了这个API,它工作得很好。
我认为PHP代码中的授权有问题,但这段代码与Gmail API一起工作得很好。
你能帮我一下吗?这是我的PHP文件:<?php
require 'google-api-client/src/Google/autoload.php';
define('APPLICATION_NAME', 'My Project');
define('CREDENTIALS_PATH', '~/.credentials/gmail.json');
define('CLIENT_SECRET_PATH', 'client_secrets.json');
define('SCOPES', implode(' ', array(
Google_Service_Webmasters::WEBMASTERS_READONLY,
Google_Service_Webmasters_SearchAnalyticsQueryRequest::NULL_VALUE,
Google_Service_Webmasters_SearchAnalyticsQueryResponse::NULL_VALUE
)
));
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %sn", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH");
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
$webmastersService = new Google_Service_Webmasters($client);
$searchanalytics = $webmastersService->searchanalytics;
$request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest;
$request->setStartDate("2015-10-01");
$request->setEndDate("2015-10-01");
$qsearch = $searchanalytics->query('{MY SITE GOES HERE}', $request);
$rows = $qsearch->getRows();
echo json_encode($rows);
-
确保你加载的文件具有搜索分析的权限,而不仅仅是gmail。
-
查询需要以下范围:
https://www.googleapis.com/auth/webmasters.readonlyhttps://www.googleapis.com/auth/webmasters
我不确定是否
Google_Service_Webmasters_SearchAnalyticsQueryRequest:: NULL_VALUE,Google_Service_Webmasters_SearchAnalyticsQueryResponse: NULL_VALUE
。