google-search-api

1.1.5 • Public • Published

google-search-api for Google Search Console

提供 Search Console API 和 Indexing API 系列工具方法,可用于在网站更新或者发布时主动告知 Google 当前站点的网站地图或者已更新的页面列表等,加快Google的收录效率。

使用 google-search-api 的前提条件

  1. 拥有 Google 帐号
  2. Google Search Console 中添加站点资源,并已验证站点所有权;
  3. 添加 wibetter 服务账号(webseo@wibetter.iam.gserviceaccount.com),并授予“拥有者”权限。

使用方法

google search api 使用方法

特别说明

  1. wibetter 服务账号 仅用于测试和验证「非正式站点」的「谷歌搜索收录推送服务」效果;
  2. 正式站点请在「Google Cloud」创建您自己的服务账号,点击创建服务账号;
  3. 国内网络环境,获取google search api的token会失败,请使用外网环境(比如在国内可使用香港机房,确保能正常连接国外网络环境);

google-search-api的使用方法

1. 安装:

$ npm install --save-dev google-search-api

或者:

$ yarn add google-search-api --dev

2. 向 Google Search Console 推送站点页面地址

import {updateSite} from 'google-search-api';
/**
 * 先获取 google search api token,再执行工具方法
 * 备注1: client_email, private_key 从 google cloud 服务账号中获取
 * 备注2: 当前示例的 client_email, private_key 从 demo/service_account.json 中获取
 */ 
const tokenInfo = await getAuthToken(client_email, private_key);
const result = await updateSite('https://www.wibetter.online/aigc85f4', tokenInfo.access_token);

3. 向 Google Search Console 批量推送站点页面地址列表

import {indexingBatch} from 'google-search-api';
// 先获取 google search api token
const tokenInfo = await getAuthToken(client_email, private_key);
const result = await indexingBatch(['https://www.wibetter.online/aigc85f4', 'https://www.wibetter.online/aigcd334'], tokenInfo.access_token);

4. 向 Google Search Console 推送站点地图

import {sumbitSiteMap} from 'google-search-api';
// 先获取 google search api token
const tokenInfo = await getAuthToken(client_email, private_key);
const result = await sumbitSiteMap(
  'https://www.wibetter.online/',
  'https://www.wibetter.online/sitemap.xml',
  tokenInfo.access_token
);

5. 查询站点推送情况,可用于检测主动推送是否生效

import {sumbitSiteMap} from 'google-search-api';
// 先获取 google search api token
const tokenInfo = await getAuthToken(client_email, private_key);
const result1 = await checkSite('https://www.wibetter.online/', tokenInfo.access_token);

返回结果说明

返回结果格式如下

{
  status: 'error/success',
  code: 200,
  codeMsg: '请求执行成功',
  message: 'xxx'
}

code 说明

code 说明
401 获取token失败,请检查服务授权配置是否正确。
403 暂无权限,可能原因:未添加站点资源 / 服务账号未授权 / 站点资源和域名不匹配。
429 请求超过配额限制。
404 找不到与该请求关联的资源。
200 请求执行成功。

更多 code 说明,请查看标准错误消息

Package Sidebar

Install

npm i google-search-api

Weekly Downloads

1

Version

1.1.5

License

MIT

Unpacked Size

205 kB

Total Files

11

Last publish

Collaborators

  • wibetter