尝试使用Node中的API生成adsense收益报告时错误403



我想从firebase云功能(在Node中)生成一个报告,我使用Adsense API v2这样:

const { google } = require("googleapis");
const adsense = google.adsense("v2");
const auth = new google.auth.GoogleAuth({
scopes: ["https://www.googleapis.com/auth/adsense", "https://www.googleapis.com/auth/adsense.readonly"],
});
const authClient = await auth.getClient();
google.options({ auth: authClient });
const res = await adsense.accounts.reports.generate({
account: "accounts/pub-1522817738845966",
dateRange: "YESTERDAY",
metrics: ["TOTAL_EARNINGS"],
dimensions: ["DATE"],
});

但是我得到以下错误:code: 403, message: ' the caller does not have permission'.

我想每天运行这个函数来检查前一天的总收益。我确保API已在Google Cloud控制台启用,并且adsense帐户与firebase管理帐户相同。

如果我在API资源管理器中输入相同的值,我得到这个响应,它没有给我想要的信息,但至少不是错误403:

200
{
"headers": [
{
"name": "DATE",
"type": "DIMENSION"
},
{
"name": "TOTAL_EARNINGS",
"type": "METRIC_CURRENCY",
"currencyCode": "EUR"
}
],
"warnings": [
"Some of the requested metrics are not available for some of the ad clients used by this report."
],
"startDate": {
"year": 2023,
"month": 4,
"day": 6
},
"endDate": {
"year": 2023,
"month": 4,
"day": 6
}
}

显然Adsense管理API不支持服务帐户,您只能使用Oauth 2认证流程

最新更新