51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

使用Indexing API通知Google检索网站

为了方便在发布新页面时推送Google搜索引擎,使用Google官方Indexing API推送。

配额

以下是项目的默认配额,如果不足可以申请提高配额。

| 资源名称 | 资源说明 | |--------------------------------------------|------------------------------------------------------------------------------------------------------| | DefaultPublishRequestsPerDayPerProject | 每个项目每天的默认配额,用于指定可以发送到 publish 端点的 publish 请求数。这包括 URL_UPDATEDURL_DELETED 请求类型。默认值设置为 200。 | | DefaultMetadataRequestsPerMinutePerProject | 每个项目每分钟的默认配额,用于指定可以发送到 getMetadata 端点的只读请求数。默认值设置为 180。 | | DefaultRequestsPerMinutePerProject | 所有端点每个项目每分钟的默认配额。默认值设置为 600。 |

对接脚本

本文档使用python3环境。

安装依赖

root@lolicp:~/# pip3 install oauth2client

批量提交url

需要具备以下条件才能执行python脚本:

  • python3环境
  • oauth2client插件
  • json格式的服务账号密钥
  • url.txt需要上传的文件

在依赖就绪后,执行脚本:

root@lolicp:~/# python3 google_Batch_Update.py
Submitted https://lolicp.com/python/202403707.html - Response: 200

脚本内容

from oauth2client.service_account import ServiceAccountCredentials
import httplib2
import json
import time

配置

SCOPES = ["https://www.googleapis.com/auth/indexing"] ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"

json密钥文件

JSON_KEY_FILE = "service_account_file.json"

存放URL的文本文件路径

URL_LIST_FILE = "url.txt"

认证

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES) http = credentials.authorize(httplib2.Http())

读取URL列表

with open(URL_LIST_FILE, "r") as file: urls = file.readlines()

去掉每个URL的换行符

urls = [url.strip() for url in urls]

遍历URL并逐一提交

for url in urls:

创建请求内容

content = { "url": url, "type": "URL_UPDATED" # URL_UPDATED 表示更新,URL_DELETED 表示移除 }

# 将字典转换为JSON字符串
body = json.dumps(content)

try:
    # 发送请求
    response, content = http.request(ENDPOINT, method="POST", body=body, headers={'Content-Type': 'application/json'})
    
    # 打印响应结果
    print(f"Submitted {url} - Response: {response['status']}")
    
    # 限制请求频率(API 限制每分钟提交的次数)
    time.sleep(2)  # 每2秒发送一个请求
except Exception as e:
    print(f"Failed to submit {url} - Error: {str(e)}")</code></pre>

报错内容

没有启用Web Search Indexing API

前往https://console.developers.google.com/apis/api/indexing.googleapis.com/overview,启用Web Search Indexing API后即可正常。

Response: {'vary': 'Origin, X-Origin, Referer', 'content-type': 'application/json; charset=UTF-8', 'date': 'Wed, 02 Oct 2024 23:25:36 GMT', 'server': 'ESF', 'cache-control': 'private', 'x-xss-protection': '0', 'x-frame-options': 'SAMEORIGIN', 'x-content-type-options': 'nosniff', 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'transfer-encoding': 'chunked', 'status': '403', 'content-length': '1044', '-content-encoding': 'gzip'}
Content: {
  "error": {
    "code": 403,
    "message": "Web Search Indexing API has not been used in project 111111111111 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/indexing.googleapis.com/overview?project=111111111111 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console API activation",
            "url": "https://console.developers.google.com/apis/api/indexing.googleapis.com/overview?project=111111111111"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "SERVICE_DISABLED",
        "domain": "googleapis.com",
        "metadata": {
          "service": "indexing.googleapis.com",
          "consumer": "projects/111111111111"
        }
      }
    ]
  }
}
错误设置用户权限

用户和权限需要设置为拥有者

Response: {'vary': 'Origin, X-Origin, Referer', 'content-type': 'application/json; charset=UTF-8', 'date': 'Wed, 02 Oct 2024 23:26:49 GMT', 'server': 'ESF', 'cache-control': 'private', 'x-xss-protection': '0', 'x-frame-options': 'SAMEORIGIN', 'x-content-type-options': 'nosniff', 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000', 'transfer-encoding': 'chunked', 'status': '403', 'content-length': '145', '-content-encoding': 'gzip'}
Content: {
  "error": {
    "code": 403,
    "message": "Permission denied. Failed to verify the URL ownership.",
    "status": "PERMISSION_DENIED"
  }
}

参考文档

https://developers.google.com/search/apis/indexing-api/v3/prereqs?hl=zh-cn https://www.sakamoto.blog/indexing-api/

赞(5)
未经允许不得转载:工具盒子 » 使用Indexing API通知Google检索网站