首页 文章

Google PHP客户端API:权限不足(searchanalytics)

提问于
浏览
0

尝试使用PHP代码中的Google SearchAnalytics API时出现以下错误:

PHP致命错误:未捕获异常'Google_Service_Exception',并显示消息'错误调用POST https://www.googleapis.com/webmasters/v3/sites/ / searchAnalytics / query:(403)Perffficient permission'in /Users/Ihar_Cheliadzinski/Workspace/DSS/google-login/google-api-client/src/Google/Http/REST.php:110

我在API Explorer中测试了这个API:https://developers.google.com/apis-explorer/?hl=en_US#p/webmasters/v3/webmasters.searchanalytics.query,它运行正常 .

我认为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%s\n", $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 %s\n", $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);

1 回答

相关问题