0

我已经创建了服务帐户,下面是我的代码:

    $client = new Google_Client();

    $client->setApplicationName("Console_Client");

    $client->setAuthConfigFile('xxaax.json');

    $scopes = [
        //"https://www.googleapis.com/auth/webmasters",
        //"https://www.googleapis.com/auth/webmasters.readonly"
        Google_Service_Webmasters::WEBMASTERS_READONLY,
        Google_Service_Webmasters::WEBMASTERS,
        ];

    $client->setScopes($scopes);

    $client->setAccessType('offline');

    $service = new Google_Service_Webmasters($client);

    $request = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();

    $optParams = array('siteUrl' => 'https://www.test.co/');

    $request->startDate = "2018-01-01";

    $request->endDate   = "2018-01-02";

    $results = $service->searchanalytics->query($optParams, $request);

我收到此错误消息作为响应:

PHP 致命错误:未捕获的 Google_Service_Exception “原因”:“insufficientPermissions”

我已经在我的帐户中启用了这个 api,并且在尝试 google API explorer 时也可以点击它。

为什么会出现这种情况?

4

1 回答 1

0

为此,我们需要创建服务帐户。当您从控制台点击 API 并且您没有任何用于 OAuth2.0 的 Web UI 时,这更可取。

对于这种特殊情况,有服务帐户 ID(看起来像长字符串电子邮件)。复制此内容并将其添加到您的谷歌帐户。做上面解决了我的问题。

这一步在 google API 文档中并不清楚。

希望这对将来的人有所帮助。

于 2018-02-26T10:55:02.207 回答