我正在尝试通过他们的 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);
... 除了在我的版本中 example.com
已替换为我的实际域。
我在命令行调用它:
GOOGLE_APPLICATION_CREDENTIALS="/path/to/service_key.json" node index.js
我创建了一个服务帐户,服务 key 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'
}
提到的网址,https://support.google.com/webmasters/answer/2451999 ,只需将我链接到搜索控制台用户页面......(再次)表示服务用户具有完全权限。
最佳答案
在谷歌论坛和互联网的其他部分 Root 后,我明白了为什么会发生这种情况。
需要从 Google Cloud: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
或执行query
API 请求时,请务必按照上述步骤设置siteUrl
,例如:http://www.example.com/
(对于 URL 前缀属性)或sc-domain:example.com
(对于域属性) 快乐编码
关于node.js - Google Search Console API : How do I Solve "User does not have sufficient permission for site"?(当用户有权限时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67306332/