【问题标题】:Google indexing API wrong json file谷歌索引 API 错误的 json 文件
【发布时间】:2021-01-28 21:34:08
【问题描述】:

您好,我正在尝试使用 Google 索引 API。我使用了这个教程 - https://developers.google.com/search/apis/indexing-api/v3/prereqs

我使用了这个 php 库 - 不同的分支 - v1-master 因为我不使用 composer 所以我不得不使用 autoload.php https://github.com/googleapis/google-api-php-client/tree/master

这是我的代码:

require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/src/Google/autoload.php');
require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/examples/templates/base.php');

$client = new Google_Client();

// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/serviceAccount');
$client->addScope('https://www.googleapis.com/auth/indexing');

// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

// Define contents here. The structure of the content is described in the next step.
$content = '{
  "url": "http://example.com/jobs/42",
  "type": "URL_UPDATED"
}';

$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
  1. 我创建了服务帐号
  2. 已下载服务帐号 json 文件 这就是 json 的样子

  1. 如上所示添加了 json 路径
  2. 我在 console.developers.google.com 中为我的项目启用了 Indexing API
  3. json 加载

但我收到错误未捕获的 Google_Exception:无效的客户端机密 JSON 文件。

这个话题与-google-api-php-client: Invalid client secret JSON file有关

但我无法解决它,我被卡住了

【问题讨论】:

    标签: php json google-indexing-api


    【解决方案1】:

    问题已解决,根据此处的问题 - https://github.com/googleapis/google-api-php-client/issues/2025#issuecomment-762634717,自动加载已被删除,整个包取决于作曲家的自动加载器(如果您正在使用它)。 如果你不使用它,作者在这里指出了最后一个带有自动加载器的可用版本

    https://github.com/googleapis/google-api-php-client/releases/tag/v2.8.2

    下载此版本并在您的代码中仅包含一个 autoload.php

    require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/vendor/autoload.php');
    

    完整代码在这里

    require_once($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/vendor/autoload.php');
    
    $client = new Google_Client();
    
    // service_account_file.json is the private key that you created for your service account.
    $client->setAuthConfig($_SERVER["DOCUMENT_ROOT"] . '/assets/google-api-php-client/mykey-pre-10b793743427.json');
    $client->addScope('https://www.googleapis.com/auth/indexing');
    
    // Get a Guzzle HTTP Client
    $httpClient = $client->authorize();
    $endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
    
    // Define contents here. The structure of the content is described in the next step.
    $content = '{
      "url": "https://my-site.sk/news.php?ID=157",
      "type": "URL_UPDATED"
    }';
    
    $response = $httpClient->post($endpoint, [ 'body' => $content ]);
    $status_code = $response->getStatusCode();
    return $status_code;
    

    也不要忘记通过点击设置 - 用户和权限 - 点击您帐户旁边的点 - 点击管理所有者 - 添加新所有者,将您的 json 中的 client_email 添加到 Search Console 所有者中(将其添加到上一个站点中的用户是不够的) - 你会得到 403 代码。 如果操作成功,您将获得代码 200,但在 Search Console 中不会发生任何变化,因为您启动了索引作业,这可能会在几个小时内运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 2015-12-14
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      相关资源
      最近更新 更多