【发布时间】:2021-12-03 12:22:18
【问题描述】:
我想使用 runReport 运行谷歌分析报告。 我已按照https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries 的说明进行操作,并在https://github.com/googleapis/nodejs-analytics-data/blob/main/samples/quickstart_json_credentials.js 复制了使用json-credentials 的代码
我有一个分析帐户,其中包含两个属性和应用程序 - DEV 和 STAGE。
在他们每个人中,我都创建了一个具有 OWNER 权限的服务帐户。之后,我创建了一个密钥并下载了生成的 JSON 文件。
我可以通过https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport 的“试用此 API”链接为我的两个属性测试和运行 API,并且它对它们都适用。 (但我当然会使用 OAuth 身份验证)。
但是,当我使用 JSON 凭据运行代码时,DEV 始终可以正常工作。 STAGE 总是失败。我尝试从我的机器和https://shell.cloud.google.com/ 运行代码,结果相同。 失败信息是:
7 PERMISSION_DENIED: User does not have sufficient permissions for this property. To learn more about Property ID, see https://developers.google.com/analytics/devguides/reporting/data/v1/property-id.
我已多次检查 DEV 和 STAGE 属性是否正确,并且我使用了与正确属性关联的 JSON 凭据文件。
在呼叫总是失败的 STAGE 中,我多次创建了新的服务帐户(使用 Basic->Owner 凭据)、创建了密钥并下载了 JSON 凭据。但结果都是一样的。 STAGE 总是失败。
我已经比较了 DEV 和 STAGE 中服务帐户的权限,它们似乎是相同的。
我已阅读并尝试过其他人在堆栈溢出时遇到“权限被拒绝”问题的提示,但没有一个能解决我的问题。
是否有我从谷歌控制台读取的任何授权日志? 我现在有点卡住了。
我运行的代码(属性和文件名被混淆):
"use strict";
function main(propertyId = "YOUR-GA4-PROPERTY-ID", credentialsJsonPath = "") {
propertyId = "27..DEV property";
credentialsJsonPath = "./DEVcredentials.json";
// propertyId = "27..STAGE property";
// credentialsJsonPath = "./STAGEcredentials.json";
const { BetaAnalyticsDataClient } = require("@google-analytics/data");
const analyticsDataClient = new BetaAnalyticsDataClient({
keyFilename: credentialsJsonPath,
});
async function runReport() {
const [response] = await analyticsDataClient.runReport({
property: `properties/${propertyId}`,
dateRanges: [
{
startDate: "2020-03-31",
endDate: "today",
},
],
dimensions: [
{
name: "country",
},
],
metrics: [
{
name: "activeUsers",
},
],
});
console.log("Report result:");
response.rows.forEach((row) => {
console.log(row.dimensionValues[0], row.metricValues[0]);
});
}
runReport();
}
process.on("unhandledRejection", (err) => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
【问题讨论】:
标签: node.js google-api google-analytics-api service-accounts google-api-nodejs-client