mac*_*ost 4 google-api node.js google-api-nodejs-client google-search-console
我正在尝试通过其 Node 包使用 Google 的 Search Console API,我的代码如下所示:
const auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/webmasters.readonly',
});
const webmasters = google.webmasters('v3');
const params = {
auth,
siteUrl: 'example.com',
resource: {
startDate: '2015-08-25',
endDate: '2015-08-25',
dimensions: ['query', 'page'],
rowLimit: 10,
},
aggregationType: 'byPage',
};
const res = await webmasters.searchanalytics.query(params);
console.log(res.data);
Run Code Online (Sandbox Code Playgroud)
...除了在我的版本中example.com已替换为我的实际域。
我在命令行中调用它:
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service_key.json" node index.js
Run Code Online (Sandbox Code Playgroud)
我创建了一个服务帐户,服务密钥 JSON 文件来自该帐户。该服务帐户可以访问我的 Search Console 帐户。当我查看https://search.google.com/search-console/users时,我看到那里的服务用户,并且在“权限”列中显示“完全”。
谁能帮助我理解为什么在运行代码时出现以下错误?
{
message: "User does not have sufficient permission for site 'http://example.com'. See also: https://support.google.com/webmasters/answer/2451999.",
domain: 'global',
reason: 'forbidden'
}
Run Code Online (Sandbox Code Playgroud)
提到的 URL https://support.google.com/webmasters/answer/2451999只是将我链接到搜索控制台用户页面...该页面(再次)表明服务用户拥有完全权限。
在扎根谷歌论坛和互联网的其他部分后,我已经弄清楚为什么会发生这种情况。
需要从 Google Cloud 复制服务帐户电子邮件,格式很长(例如:whiskey-tango-foxtrot@certain-something-0123456.iam.gserviceaccount.com):https: //console.cloud.google.com/apis/credentials以“完整站点”用户身份访问 Search Console https://search.google.com/u/1/search-console/users 。
确保您已通过 API 或使用此工具添加了该网站(从 Search Console 上): https: //developers.google.com/webmaster-tools/v1/sites/add
然后,当您执行“list_sites”请求时,您的网站应该列出,并且permission_level是“siteFullUser”(根据步骤1)
当您add_site或执行queryAPI 请求时,请确保siteUrl按照上述步骤进行设置,例如:(http://www.example.com/对于 URL 前缀属性)或sc-domain:example.com(对于域属性)快乐编码
| 归档时间: |
|
| 查看次数: |
1510 次 |
| 最近记录: |