Jan*_*may 5 google-api google-webmaster-tools google-api-php-client
我使用google-api-php-client库来访问网站管理员工具数据.当我想列出站点地图时,它出现了致命错误:未捕获的异常'Google_Service_Exception'(403)用户没有足够的站点权限.另请参阅:https://support.google.com/webmasters/answer/2451999.' 我将服务帐户电子邮件地址添加为我的网站的限制用户,但错误仍然存在.
最后我找到答案:服务帐户不像普通的Google帐户.即使您将特定地址"访问",也无法使用它来访问特定资源.请参阅此处,了解您可以通过哪些方式授权您对网站站长API的请求.
Mar*_*vdB 20
对我来说,问题不是实际的权限,而是域名的传递方式。
您应该为您的域名添加前缀sc-domain::sc-domain:yourdomain.com。仅传递不带sc-domain:前缀的“yourdomain.com”将导致错误User does not have sufficient permission for site yourdomain.com。
这是我的 Node.js 示例,但 PHP 也是如此:
import {JWT} from 'google-auth-library';
const client = new JWT(
null,
'service-account.json',
null,
['https://www.googleapis.com/auth/webmasters.readonly'],
);
const res = await client.request({
url: 'https://www.googleapis.com/webmasters/v3/sites/sc-domain:yourdomain.com/searchAnalytics/query',
method: 'POST',
data: {
"startDate": "2020-04-01",
"endDate": "2020-05-01",
"dimensions": ["country", "device"]
}
});
console.log(res.data);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2420 次 |
| 最近记录: |