SEO 2026年3月29日

让你的网站被 Google 秒收录:Indexing API + Sitemap Ping 5分钟教程

不想等 Google 慢悠悠爬过来?用 Google Indexing API 主动提交新页面,实测 2-6 小时内完成收录,每天 200 个免费额度。

W

Woody

woodyrush.com

让你的网站被 Google 秒收录:Indexing API + Sitemap Ping 5分钟教程

发了新文章,等 Google 慢悠悠爬过来?不用等了。这篇教你用 Google Indexing API 主动”敲门”,让 Google 几小时内就来收录。


为什么需要这个?

正常情况下,你发布一篇新文章,Google 可能 3-7 天后才会发现并收录。

但 Google 提供了一个免费 API——Indexing API,让你主动告诉 Google:“嘿,我这有新内容,快来看。”

  • 效果:几小时内收录,而不是几天
  • 每天有 200 个 URL 的免费额度,对中小网站完全够用

整体架构(30秒看懂)

你发布文章

脚本自动扫描最近发布的文章

调用 Google Indexing API 提交 URL

同时 Ping Google/Bing 的 Sitemap 端点

Google 几小时内来爬你的页面

需要准备的东西:

  • 一个 Google Cloud 项目(免费)
  • 一个 Service Account(免费)
  • Node.js 环境
  • 你网站的 Google Search Console 权限

Step 1:创建 Google Service Account(2分钟)

1.1 打开 Google Cloud Console,创建一个项目(或用已有的)

1.2 左侧菜单 → IAM & Admin → Service Accounts → Create Service Account

  • 名称随便填,比如 my-site-indexing
  • 角色不用选,直接完成

1.3 点进刚创建的 Service Account → Keys → Add Key → Create new key → 选 JSON → 下载

你会得到一个 .json 文件,长这样:

{
  "type": "service_account",
  "project_id": "your-project",
  "private_key": "-----BEGIN PRIVATE KEY-----\n...",
  "client_email": "[email protected]"
}

记住那个 client_email,后面要用。

1.4 启用 Indexing API:打开 Indexing API 页面,点 Enable


Step 2:给 Service Account 添加 Search Console 权限(1分钟)

这步是关键——Google 要求调用 Indexing API 的账号必须是网站的 Owner

但 Google Search Console 的 UI 不允许直接把 Service Account 添加为 Owner(因为 Service Account 没有登录界面)。

解决办法:用 Google OAuth Playground 通过 API 添加。

2.1 打开 OAuth Playground

2.2 左侧找到 Site Verification API v1 → 勾选 https://www.googleapis.com/auth/siteverification

2.3 点 Authorize APIs → 用你的 Google 账号(已验证的网站所有者)登录

2.4 点 Exchange authorization code for tokens

2.5 在 Step 3 填写:

  • HTTP Method: PUT
  • Request URI:
https://www.googleapis.com/siteVerification/v1/webResource/https%3A%2F%2Fyour-site.com%2F

(把 your-site.com 换成你的域名)

  • Content-Type: application/json
  • Request Body:
{
  "owners": ["[email protected]"],
  "site": {
    "type": "SITE",
    "identifier": "https://your-site.com/"
  }
}

2.6 点 Send the request → 返回 200 就成功了


Step 3:写提交脚本(2分钟)

安装依赖:

npm install googleapis

创建 submit-indexing.ts(或 .js):

import { google } from 'googleapis';

// 你要提交的 URL 列表
const URLS = [
  'https://your-site.com/',
  'https://your-site.com/blog/your-new-post',
];

async function main() {
  // 读取 Service Account 密钥
  const credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_KEY || '{}');

  const auth = new google.auth.GoogleAuth({
    credentials,
    scopes: ['https://www.googleapis.com/auth/indexing'],
  });

  const indexing = google.indexing({ version: 'v3', auth });

  for (const url of URLS) {
    try {
      await indexing.urlNotifications.publish({
        requestBody: { url, type: 'URL_UPDATED' },
      });
      console.log(`✓ 提交成功: ${url}`);
    } catch (error) {
      console.warn(`✗ 提交失败: ${url}`, error.message);
    }
  }
}

main();

运行:

GOOGLE_SERVICE_ACCOUNT_KEY="$(cat your-service-account.json)" npx tsx submit-indexing.ts

看到 ✓ 提交成功 就对了。


Step 4:加上 Sitemap Ping(30秒)

这个更简单,一个文件搞定:

// ping-sitemap.ts
const SITEMAP = 'https://your-site.com/sitemap.xml';

const endpoints = [
  `https://www.google.com/ping?sitemap=${encodeURIComponent(SITEMAP)}`,
  `https://www.bing.com/ping?sitemap=${encodeURIComponent(SITEMAP)}`,
];

for (const url of endpoints) {
  try {
    const res = await fetch(url, { signal: AbortSignal.timeout(10000) });
    console.log(`✓ Pinged ${new URL(url).hostname}: ${res.status}`);
  } catch (e) {
    console.warn(`✗ Failed: ${new URL(url).hostname}`);
  }
}

⚠️ 注意:Google 的 ping 端点近期返回 404 是已知情况(Google 在逐步弃用这个端点),但 Bing 的仍然有效。有 Indexing API 在,ping 只是锦上添花。


Step 5:自动化(可选但推荐)

如果你用 GitHub Actions 部署网站,在 workflow 里加两步:

# 在部署步骤之后添加
- name: 提交新页面到 Google
  continue-on-error: true
  run: npx tsx submit-indexing.ts
  env:
    GOOGLE_SERVICE_ACCOUNT_KEY: ${{ secrets.GOOGLE_SERVICE_ACCOUNT_KEY }}

- name: Ping Sitemap
  continue-on-error: true
  run: npx tsx ping-sitemap.ts

把 Service Account JSON 的内容存到 GitHub Secrets 里(Settings → Secrets → GOOGLE_SERVICE_ACCOUNT_KEY)。

continue-on-error: true 很重要——即使提交失败也不会影响你的部署流程。


进阶:自动发现新文章

上面的脚本需要手动填 URL。如果你的网站用 Markdown 写文章(比如 Astro、Next.js、Hugo),可以让脚本自动扫描最近发布的文章:

import { readdirSync, readFileSync } from 'fs';

function findRecentArticles(contentDir: string, hoursBack = 48) {
  const urls: string[] = [];
  const cutoff = Date.now() - hoursBack * 3600 * 1000;

  for (const file of readdirSync(contentDir)) {
    if (!file.endsWith('.md')) continue;
    const content = readFileSync(`${contentDir}/${file}`, 'utf-8');

    // 从 frontmatter 提取 pubDate
    const match = content.match(/pubDate:\s*(.+)/);
    if (!match) continue;

    const pubDate = new Date(match[1].trim());
    if (pubDate.getTime() > cutoff) {
      const slug = file.replace('.md', '');
      urls.push(`https://your-site.com/blog/${slug}`);
    }
  }

  return urls;
}

这样每次发文章,pipeline 自动发现、自动提交,完全不用管。


总结

步骤耗时做什么
创建 Service Account2 minGoogle Cloud Console
添加 Owner 权限1 minOAuth Playground API 调用
写提交脚本1 min复制粘贴上面的代码
Sitemap Ping30 sec一个 fetch 搞定
接入 CI/CD30 secworkflow 加两行

成本:$0。效果:收录速度从几天变成几小时。

唯一的限制是每天 200 个 URL 额度,但除非你是新闻站日更百篇,否则完全够用。

#google #indexing-api #sitemap #seo #search-console