【问题标题】:Google Analytics Webmaster API getting insufficientPermissions error code :403Google Analytics Webmaster API 获取不足权限错误代码:403
【发布时间】:2018-02-16 15:53:27
【问题描述】:

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

    $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 “原因”:“权限不足”

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

为什么会这样?

【问题讨论】:

    标签: php google-analytics-api google-api-webmasters


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 1970-01-01
      • 2014-06-01
      • 2014-09-06
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2021-01-18
      相关资源
      最近更新 更多