在开始使用 Indexing API 之前,需要先执行以下几项操作(如果尚未完成):
为客户端创建一个项目
在向 Indexing API 发送请求之前,您需要告知 Google 您的客户端并激活对 Indexing API 的访问权限。要完成这项操作,您可以使用 Google API 控制台创建一个项目(该项目是设置和 API 访问信息的命名集合),然后注册您的应用。
开始使用 Indexing API 之前,您需要先使用设置工具在 Google API 控制台中创建项目、启用 Indexing API 以及创建凭据。
创建服务帐号
- 打开服务帐号页面。如果看到提示,请选择项目。
- 点击 创建服务帐号,并输入服务帐号的名称和说明。您可以使用默认服务帐号 ID,也可以选择其他唯一的帐号 ID。完成后,点击创建。
- 后面的服务帐号权限(可选)部分无需设置。点击继续。
- 在向用户授予访问此服务帐号的权限屏幕上,向下滚动到创建密钥部分。点击 创建密钥。
- 在随即显示的侧面板中,选择密钥的格式:建议使用 JSON。
- 点击创建。您的新公钥/私钥对随后会生成并下载到您的计算机上;该密钥仅此一份。要了解如何安全地存储密钥,请参阅管理服务帐号密钥。
- 点击私钥已保存到您的计算机对话框中的关闭,然后点击完成以返回服务帐号表格。
仅当您想要向服务帐号授予 G Suite 全网域委派权限时,才需要执行以下步骤:
- 在表格中找到新创建的服务帐号。在操作下方,点击 ,然后点击修改。
- 在服务帐号详情中,点击 显示全网域委派,然后确保已选中启用 G Suite 全网域委派功能复选框。
- 如果您尚未配置应用的 OAuth 同意屏幕,则必须先配置此屏幕,然后才能启用全网域委派。按照屏幕上的说明配置 OAuth 同意屏幕,然后重复上述步骤并重新选中复选框。
- 点击保存以更新服务帐号,然后返回服务帐号表格。此时,您可以看到一个新的列,即全网域委派功能。点击查看客户端 ID,获取并记录下客户端 ID。
在 Search Console 中验证网站所有权
在此步骤中,您将验证您对自己的网络资源拥有控制权。
如需验证您对网站的所有权,请执行以下操作:
- 按照建议步骤操作,验证您对资源的所有权。
- 验证资源后,打开 Search Console。
- 选择您已验证的资源。
- 从左侧导航栏中打开该资源的设置。
- 选择用户和权限。点击添加所有者。
- 输入您的服务帐号电子邮件地址。在“权限”下拉列表中,选择完整。您可以在以下两个位置找到您的服务帐号电子邮件地址:
- 您在创建项目时下载的 JSON 私钥中的
client_email
字段。 - Developers Console 中“服务帐号”视图的服务帐号 ID 列。
电子邮件地址的格式类似如下:
my-service-account@project-name.google.com.iam.gserviceaccount.com
例如,“my-service-account@test-project-42.google.com.iam.gserviceaccount.com”。
- 您在创建项目时下载的 JSON 私钥中的
获取访问令牌
要调用 Indexing API,必须使用您获得的 OAuth 令牌对调用进行身份验证,以换取您的私钥。Google 提供 API 客户端库,让您可通过多种语言获取 Oauth 令牌。
要求
在向 Indexing API 提交请求时,您的请求必须满足以下条件:
- 使用
https://www.googleapis.com/auth/indexing
作为作用域。 - 使用使用 API 中介绍的端点之一。
- 包含服务帐号访问令牌。
- 按照使用 API 中的说明定义请求的正文。
示例
以下示例说明了如何获取 OAuth 访问令牌:
Python
使用 Python 版 Google API 客户端库获取 OAuth 令牌:
from oauth2client.service_account import ServiceAccountCredentials import httplib2 SCOPES = [ "https://www.googleapis.com/auth/indexing" ] ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish" # service_account_file.json is the private key that you created for your service account. JSON_KEY_FILE = "service_account_file.json" credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES) http = credentials.authorize(httplib2.Http()) # Define contents here as a JSON string. # This example shows a simple update request. # Other types of requests are described in the next step. content = """{ \"url\": \"http://example.com/jobs/42\", \"type\": \"URL_UPDATED\" }""" response, content = http.request(ENDPOINT, method="POST", body=content)
Java
使用 Java 版 API 客户端库获取 OAuth 令牌:
String scopes = "https://www.googleapis.com/auth/indexing"; String endPoint = "https://indexing.googleapis.com/v3/urlNotifications:publish"; JsonFactory jsonFactory = new JacksonFactory(); // service_account_file.json is the private key that you created for your service account. InputStream in = IOUtils.toInputStream("service_account_file.json"); GoogleCredential credentials = GoogleCredential.fromStream(in, this.httpTransport, jsonFactory).createScoped(Collections.singleton(scopes)); GenericUrl genericUrl = new GenericUrl(endPoint); HttpRequestFactory requestFactory = this.httpTransport.createRequestFactory(); // Define content here. The structure of the content is described in the next step. String content = "{" + "\"url\": \"http://example.com/jobs/42\"," + "\"type\": \"URL_UPDATED\"," + "}"; HttpRequest request = requestFactory.buildPostRequest(genericUrl, ByteArrayContent.fromString("application/json", content)); credentials.initialize(request); HttpResponse response = request.execute(); int statusCode = response.getStatusCode();
PHP
使用 PHP 版 API 客户端库获取 OAuth 令牌:
require_once '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('service_account_file.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": "http://example.com/jobs/42", "type": "URL_UPDATED" }'; $response = $httpClient->post($endpoint, [ 'body' => $content ]); $status_code = $response->getStatusCode();
Node.js
使用 Node.js 客户端库获取 OAuth 令牌:
var request = require("request"); var { google } = require("googleapis"); var key = require("./service_account.json"); const jwtClient = new google.auth.JWT( key.client_email, null, key.private_key, ["https://www.googleapis.com/auth/indexing"], null ); jwtClient.authorize(function(err, tokens) { if (err) { console.log(err); return; } let options = { url: "https://indexing.googleapis.com/v3/urlNotifications:publish", method: "POST", // Your options, which must include the Content-Type and auth headers headers: { "Content-Type": "application/json" }, auth: { "bearer": tokens.access_token }, // Define contents here. The structure of the content is described in the next step. json: { "url": "http://example.com/jobs/42", "type": "URL_UPDATED" } }; request(options, function (error, response, body) { // Handle the response console.log(body); }); });
除了演示如何获取令牌之外,这些示例还演示了您可以添加请求消息正文的位置。要了解您可以发出哪些类型的调用以及这些调用的消息正文结构,请参阅使用 Indexing API。