透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 Indexing API 的事前準備
使用 Indexing API 之前,您必須先完成以下程序:
為用戶端建立專案
您必須先提供用戶端資訊給 Google,並啟用 Indexing API 存取權,才能對 API 傳送要求。如要完成上述操作,請透過 Google API 控制台建立一個「專案」(專案是已命名的集合,其中含有各種設定及 API 存取權的相關資訊),並註冊您的應用程式。
如要開始使用 Indexing API,請使用設定工具;這項工具會逐步引導您在 Google API 控制台中建立專案、啟用 API,並建立憑證。
建立服務帳戶
- 開啟「服務帳戶」頁面。如果出現系統提示,請選取您要使用的專案。
- 按一下 [add 建立服務帳戶],然後輸入服務帳戶的名稱和說明。您可以使用預設的服務帳戶 ID,也可以自行選擇其他不重複的名稱。完成後,請按一下 [建立]。
- 系統會隨即顯示「服務帳戶權限」部分,不過您不一定要設定這些權限。請按一下 [繼續]。
- 在「將這個服務帳戶的存取權授予使用者」畫面中,向下捲動至「建立金鑰」部分。按一下 [add 建立金鑰]。
- 在隨即顯示的側邊面板中選取金鑰格式;建議您選擇 [JSON]JSON。
- 按一下「建立」。接著,系統就會為您產生一對新的公開/私密金鑰,並下載至您的電腦中;這是金鑰的唯一副本,如要瞭解安全儲存的方式,請參閱管理服務帳戶金鑰。
- 在 [已將私密金鑰儲存至您的電腦中] 對話方塊中按一下 [關閉],然後再按一下 [完成],即可返回您的服務帳戶表格。
將服務帳戶新增為網站擁有者
如何將服務帳戶新增為網站擁有者:
- 請先透過 Search Console 驗證網站擁有權,然後
- 將服務帳戶新增為擁有者。
1. 證明您是網站的擁有者
透過 Search Console 驗證網站擁有權。您可以使用 Search Console 支援的任何驗證方法,也可以建立網域資源 (example.com
) 或網址前置字元資源 (https://example.com
或 https://example.com/some/path/
) 來代表網站 (請注意,網站在 Search Console 中稱為「資源」)。
2. 將擁有者狀態授予服務帳戶
接下來,請將服務帳戶新增為 (委派) 網站擁有者:
- 開啟 Search Console。
- 按一下您已驗證擁有權的資源。
- 在 [已驗證擁有者] 清單中,按一下 [新增擁有者]。
- 提交做為委派擁有者的服務帳戶電子郵件。您可以在以下兩個位置找到服務帳戶的電子郵件地址:
- 建立專案時所下載 JSON 私密金鑰中的
client_email
欄位。
- Google Cloud 控制台中,「服務帳戶」檢視畫面的「服務帳戶 ID」欄。
這個電子郵件地址的格式如下:
my-service-account@project-name.google.com.iam.gserviceaccount.com
範例:my-service-account@test-project-42.google.com.iam.gserviceaccount.com
取得存取憑證
所有傳送至 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);
});
});
上述範例除了示範如何取得憑證,也說明了可以在哪裡加入要求訊息的主體。如要瞭解您可以傳送的呼叫類型有哪些,以及這些呼叫的訊息主體結構為何,請參閱使用 API 一文。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-04 (世界標準時間)。
[null,null,["上次更新時間:2025-08-04 (世界標準時間)。"],[[["\u003cp\u003eTo utilize the Indexing API, you must first create a project, a service account, and add the service account as a site owner in Search Console.\u003c/p\u003e\n"],["\u003cp\u003eAfter setup, each API call needs an OAuth token, which you get in exchange for your private key using Google's API client libraries and adhering to specific requirements.\u003c/p\u003e\n"],["\u003cp\u003eWhen making a request, you must define the request body and use the appropriate endpoint as detailed in the Indexing API documentation's "Using the API" section.\u003c/p\u003e\n"],["\u003cp\u003eGoogle provides API client libraries in multiple programming languages, such as Python, Java, PHP, and Node.js, to simplify the process of getting OAuth tokens and making API calls.\u003c/p\u003e\n"]]],["To use the Indexing API, you must first create a project in the Google API Console, create a service account with a JSON key, and then add this service account as a delegated owner of your site in Search Console. Finally, get an OAuth access token using your private key to authenticate API calls. Each request must use `https://www.googleapis.com/auth/indexing` as the scope, and include the service account access token. The content gives examples in different programming languages.\n"],null,["Prerequisites for the Indexing API\n\nBefore you can start using the Indexing API, there are a few\nthings you need to do, if you haven't done them already:\n\n- [Create a project for your client](#create-project)\n- [Create a service account](#create-service-account)\n- [Add your service account as a site owner](#verify-site)\n- [Get an access token](#oauth)\n\nCreate a project for your client\n\nBefore you can send requests to the Indexing API, you need to tell Google about your client and\nactivate access to the API. You do this by using the Google API Console to create a project,\nwhich is a named collection of settings and API access information, and register your application.\n\nTo get started using Indexing API, you need to first\n[use\nthe setup tool](https://console.cloud.google.com/start/api?id=indexing.googleapis.com&credential=client_key), which guides you through creating a project in the\nGoogle API Console, enabling the API, and creating credentials.\n\nCreate a service account\n\n1. Open the [**Service accounts** page](https://console.cloud.google.com/iam-admin/serviceaccounts). If prompted, select a project.\n2. Click add **Create Service Account** , enter a name and description for the service account. You can use the default service account ID, or choose a different, unique one. When done click **Create**.\n3. The **Service account permissions (optional)** section that follows is not required. Click **Continue**.\n4. On the **Grant users access to this service account** screen, scroll down to the **Create key** section. Click add **Create key**.\n5. In the side panel that appears, select the format for your key: **JSON** is recommended.\n6. Click **Create** . Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. For information on how to store it securely, see [Managing service account keys](https://cloud.google.com/iam/docs/understanding-service-accounts#managing_service_account_keys).\n7. Click **Close** on the **Private key saved to your computer** dialog, then click **Done** to return to the table of your service accounts.\n\nAdd your service account as a site owner\n\n\nTo add your service account as a site owner:\n\n1. First prove that you own the site, using Search Console, then\n2. Add your service account as an owner.\n\n1. Prove that you own the site\n\n\n[Verify ownership of your site](https://support.google.com/webmasters/answer/9008080)\nusing Search Console.\nYou can use any verification method supported by Search Console. You can create either\na Domain property (`example.com`) or a URL-prefix property (`https://example.com`\nor `https://example.com/some/path/`) to represent your site (note that sites are called\n*properties* in Search Console).\n\n2. Grant owner status to your service account\n\nNext, add your service account as a\n([delegated](https://support.google.com/webmasters/answer/7687615#permissions-section))\nsite owner:\n\n1. Open [Search Console](https://www.google.com/webmasters/verification/home).\n2. Click the property for which you verified ownership.\n3. In the **Verified owner** list, click **Add an owner**.\n4. Provide your service account email as the delegated owner. You can find your service account email address in two places:\n - The `client_email` field in the JSON private key that you downloaded when you [created your project](#create-project).\n - The **Service account ID** column of the Service Accounts view in the Google Cloud console.\n\n The email address has a format like this: \n \u003cvar translate=\"no\"\u003emy-service-account\u003c/var\u003e`@`\u003cvar translate=\"no\"\u003eproject-name\u003c/var\u003e`.google.com.iam.gserviceaccount.com` \n **For example:** my-service-account@test-project-42.google.com.iam.gserviceaccount.com\n\nGet an access token\n\nEvery call to the Indexing API must be authenticated with an OAuth token that you get\nin exchange for your private key. Each token is good for a span of time.\nGoogle provides [API client libraries](/api-client-library)\nto get OAuth tokens for a number of languages.\n\nRequirements\n\nWhen submitting a request to the Indexing API, your request must:\n\n1. Use `https://www.googleapis.com/auth/indexing` as the scope.\n2. Use one of the endpoints described in [Using the API](/search/apis/indexing-api/v3/using-api).\n3. Include the [service account access token](#create-service-account).\n4. Define the body of the request as described in [Using the API](/search/apis/indexing-api/v3/using-api).\n\nExamples\n\nThe following examples show how to obtain an OAuth access token: \n\nPython\n\nObtains an OAuth token using the [Google API Client library for Python](/api-client-library/python): \n\n```python\nfrom oauth2client.service_account import ServiceAccountCredentials\nimport httplib2\n\nSCOPES = [ \"https://www.googleapis.com/auth/indexing\" ]\nENDPOINT = \"https://indexing.googleapis.com/v3/urlNotifications:publish\"\n\n# service_account_file.json is the private key that you created for your service account.\nJSON_KEY_FILE = \"service_account_file.json\"\n\ncredentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)\n\nhttp = credentials.authorize(httplib2.Http())\n\n# Define contents here as a JSON string.\n# This example shows a simple update request.\n# Other types of requests are described in the next step.\n\ncontent = \"\"\"{\n \\\"url\\\": \\\"http://example.com/jobs/42\\\",\n \\\"type\\\": \\\"URL_UPDATED\\\"\n}\"\"\"\n\nresponse, content = http.request(ENDPOINT, method=\"POST\", body=content)\n```\n\nJava\n\nObtains an OAuth token using the [API Client Library for Java](/api-client-library/java): \n\n```java\nString scopes = \"https://www.googleapis.com/auth/indexing\";\nString endPoint = \"https://indexing.googleapis.com/v3/urlNotifications:publish\";\n\nJsonFactory jsonFactory = new JacksonFactory();\n\n// service_account_file.json is the private key that you created for your service account.\nInputStream in = IOUtils.toInputStream(\"service_account_file.json\");\n\nGoogleCredential credentials =\n GoogleCredential.fromStream(in, this.httpTransport, jsonFactory).createScoped(Collections.singleton(scopes));\n\nGenericUrl genericUrl = new GenericUrl(endPoint);\nHttpRequestFactory requestFactory = this.httpTransport.createRequestFactory();\n\n// Define content here. The structure of the content is described in the next step.\nString content = \"{\"\n + \"\\\"url\\\": \\\"http://example.com/jobs/42\\\",\"\n + \"\\\"type\\\": \\\"URL_UPDATED\\\",\"\n + \"}\";\n\nHttpRequest request =\n requestFactory.buildPostRequest(genericUrl, ByteArrayContent.fromString(\"application/json\", content));\n\ncredentials.initialize(request);\nHttpResponse response = request.execute();\nint statusCode = response.getStatusCode();\n```\n\nPHP\n\nObtains an OAuth token using the [API Client Library for PHP](/api-client-library/php): \n\n```php\nrequire_once 'google-api-php-client/vendor/autoload.php';\n\n$client = new Google_Client();\n\n// service_account_file.json is the private key that you created for your service account.\n$client-\u003esetAuthConfig('service_account_file.json');\n$client-\u003eaddScope('https://www.googleapis.com/auth/indexing');\n\n// Get a Guzzle HTTP Client\n$httpClient = $client-\u003eauthorize();\n$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';\n\n// Define contents here. The structure of the content is described in the next step.\n$content = '{\n \"url\": \"http://example.com/jobs/42\",\n \"type\": \"URL_UPDATED\"\n}';\n\n$response = $httpClient-\u003epost($endpoint, [ 'body' =\u003e $content ]);\n$status_code = $response-\u003egetStatusCode();\n```\n\nNode.js\n\nObtains an OAuth token using the [Node.js Client Library](https://github.com/google/google-api-nodejs-client): \n\n```javascript\nvar request = require(\"request\");\nvar { google } = require(\"googleapis\");\nvar key = require(\"./service_account.json\");\n\nconst jwtClient = new google.auth.JWT(\n key.client_email,\n null,\n key.private_key,\n [\"https://www.googleapis.com/auth/indexing\"],\n null\n);\n\njwtClient.authorize(function(err, tokens) {\n if (err) {\n console.log(err);\n return;\n }\n let options = {\n url: \"https://indexing.googleapis.com/v3/urlNotifications:publish\",\n method: \"POST\",\n // Your options, which must include the Content-Type and auth headers\n headers: {\n \"Content-Type\": \"application/json\"\n },\n auth: { \"bearer\": tokens.access_token },\n // Define contents here. The structure of the content is described in the next step.\n json: {\n \"url\": \"http://example.com/jobs/42\",\n \"type\": \"URL_UPDATED\"\n }\n };\n request(options, function (error, response, body) {\n // Handle the response\n console.log(body);\n });\n});\n```\n\nIn addition to showing how to obtain a token, these examples show where you can add the body of\nthe request message. For information about the types of calls you can make, and the structure of the\nmessage bodies for those calls, see [Using the API](/search/apis/indexing-api/v3/using-api)."]]