我正在尝试使用 Google youtube 分析 API,但我的请求收到了以下响应:
{ code: 403,
message: 'Forbidden',
errors: [ { message: 'Forbidden', domain: 'global', reason: 'forbidden' } ]
}
在此之前,我收到回复说我需要为我的服务帐户启用 youtube 分析服务(我这样做了)。
这是什么原因造成的?当原因只是“禁止”时,我应该如何调试它?
这是创建请求的代码:
const result = await youtubeAnalytics.reports.query({
"ids": "channel==MINE",
"startDate": "2017-01-01",
"endDate": "2017-12-31",
"metrics": "views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained",
"dimensions": "day",
"sort": "day"
})
这是创建youtubeAnalytics实例的代码:
const { google } = require('googleapis');
const googleServiceAccount = require('../google-service-account.dev');
const scopes = [
'https://www.googleapis.com/auth/youtube.readonly'
];
const jwtClient = new google.auth.JWT(
googleServiceAccount.client_email,
null,
googleServiceAccount.private_key,
scopes,
null
);
const youtubeAnalytics = google.youtubeAnalytics({
version: 'v2',
auth: jwtClient,
});
该服务帐户是通过我的谷歌开发控制台创建的,并且具有“项目 > 查看器”角色。
编辑:尝试切换scope到,https://www.googleapis.com/auth/yt-analytics.readonly因为我正在尝试访问 youtube 分析(结果相同)。
编辑 2:还尝试使用 youtube 数据 API 来查看它是否可以正常工作。我可以向数据 API 发送搜索请求,但我似乎无法向分析 API 发送查询请求(这适用于两个范围)。
编辑 3:当我添加范围时,我的工作很短暂,https://www.googleapis.com/auth/yt-analytics-monetary.readonly但现在我又回到了同样无用的 403“禁止”响应。