我正在尝试将自定义社区连接器连接到 google analytics 4,以便从分析中获取数据并能够在应用脚本中对其进行修改,然后将其发送到数据工作室。但是,我在连接 Google Analytics 和从中检索数据时遇到困难。
错误:
{ error:
{ code: 403,
message: 'Google Analytics Data API has not been used in project 640397821842 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsdata.googleapis.com/overview?project=640397821842 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.',
status: 'PERMISSION_DENIED',
details: [ [Object], [Object] ] } }
我已经设置了范围
"oauthScopes": [
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/analytics.readonly"
],
我要测试的代码:
function testFetch(){
const options = {
entry: { propertyId: 263290444},
"dateRanges": [{ "startDate": "2020-12-01", "endDate": "2021-03-01" }],
"dimensions": [{ "name": "country" }],
"metrics": [{ "name": "activeUsers" }],
// etc.
}
var response = UrlFetchApp.fetch(
'https://analyticsdata.googleapis.com/v1alpha:runReport', {
method: 'POST',
muteHttpExceptions: true,
headers: {
Authorization : `Bearer ${ScriptApp.getOAuthToken()}`
},
contentType: 'application/json; charset=utf-8',
payload: JSON.stringify(options)
});
var result = JSON.parse(response.getContentText());
console.log(result);
最佳答案
'Google Analytics Data API has not been used in project 640397821842 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsdata.googleapis.com/overview?project=640397821842 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.', status:
您遇到的问题是您尚未在 Google 开发人员控制台中启用该 API 您需要点击错误消息中的链接,然后转到库并查找 Google 分析数据 API 并启用它。
启用它后,尝试再次运行您的应用程序。
关于google-apps-script - 尝试从应用程序脚本获取谷歌分析 4 数据时出现 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66496448/