gpt4 book ai didi

Node.js:Google Analytics API:如何使用 "service account"发出授权的只读管理 API 请求?

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:58 25 4
gpt4 key购买 nike

我正在创建一个在 Raspberry Pi 上运行的 LED 网格行情自动收录器它将使用 Node.js 通过 Google Analytics API 收集 Google Analytics 数据。它将统计来自多个网站的计数,然后在网格上滚动统计数据。

我可以通过 Management API 使用帮助列出帐户项目,特别是帐户 ID、属性 ID 和 View ID,以节省手动工作。我在这方面遇到错误(见下文)。

这似乎是服务帐户的工作。设置 Google Cloud 项目后 here ,然后设置服务帐户 here ,我将下载的授权中包含的电子邮件地址添加为属性用户(读取/分析权限)(如果适用于 Google Analytics 管理设置)。有了这个,我可以成功检索已知 View ID 的 Analytics 数据(在 Google Analytics 管理中手动查找,ID 显示在 View 设置页面上),方法如下:

'use strict';

//////////////
// includes //
//////////////

var google = require('googleapis');
var fs = require('fs');
var Promise = require('promise');

//////////////
// settings //
//////////////

//the service account auth file downloaded from Google Cloud
var serviceAccountInfoPath = './secrets.json';

//view id with 'ga:' prefix
//I want this to soon be an array of ids looked up by an authenticated API requested
var viewID = 'ga:999999999';

////////
// go //
////////

// go get it all
var go = function() {
loadServiceAccountInfo()
.then(function(serviceAccountInfo) {
return getJwtClient(serviceAccountInfo);
})
.then(function(jwtClient) {
var client = jwtClient;
var analytics = google.analytics('v3');
return getThisYearViewAnalytics(client, analytics, viewID);
})
.then(function(result) {
console.log('total users: ' + result.totalsForAllResults["ga:users"]);
console.log('total sessions: ' + result.totalsForAllResults["ga:pageviews"]);
console.log('total pageviews: ' + result.totalsForAllResults["ga:sessions"]);
})
.catch(function(error) {
console.log("go promise chain failed", error);
});
};

// load the Google service account login details
var loadServiceAccountInfo = function() {
return new Promise(function(resolve, reject) {
fs.readFile(serviceAccountInfoPath, 'utf8', function(error, serviceAccountInfoData) {
if(error) {
console.log('loadServiceAccountInfo failed: ' + error);
reject(error);
} else {
var serviceAccountInfo = JSON.parse(serviceAccountInfoData);
resolve(serviceAccountInfo);
}
});
});
};

//return a an authenticated Google API client
var getJwtClient = function(serviceAccountInfo) {
return new Promise(function(resolve, reject) {
var jwtClient = new google.auth.JWT(serviceAccountInfo.client_email, null, serviceAccountInfo.private_key, ['https://www.googleapis.com/auth/analytics.readonly'], null);
jwtClient.authorize(function (error, tokens) {
if (error) {
console.log('getJwtClient failed: ' + error);
reject(error);
} else {
resolve(jwtClient);
}
});
});
};

//this is a test query to get the last year of data for a single view id
var getThisYearViewAnalytics = function(client, analytics, viewID) {
return new Promise(function(resolve, reject) {
analytics.data.ga.get({
'auth': client,
'ids': viewID,
'metrics': 'ga:sessions,ga:pageviews,ga:users',
'start-date': '365daysAgo',
'end-date': 'today',
},
function (error, response) {
if (error) {
console.log('getThisYearViewAnalytics failed: ' + error);
reject(error);
} else {
resolve(response);
}
});
});
};

//ready, set, go!
go();

这是一个很好的进步,但是当我尝试 list accounts 时,我开始遇到身份验证错误。 ,其中包含列出 View 所需的 id(如果您知道帐户 id,也会给出相同的错误)。

我猜答案是我需要使用 OAuth,但如果是这样,怎么办?服务帐户不再是一种选择吗?当服务帐户和硬件设备没有通常的 OAuth 来回用户交互时,我不确定如何继续进行此操作。也许我只是在上面使用的 JWT 客户端中设置了一些错误?

UDP日期:

为将来阅读此内容的人添加了信息:在确保正确设置读取权限后,我能够使用以下内容列出 Google Analytics(分析)帐户:

analytics.management.accounts.list({'auth': client}, function(error, response) {
console.log(error);
console.log(response);
});

注意:这并不遵循我上面使用的 promise 模式,但给出了 Node.js googleapis 模块如何工作的想法。

最佳答案

将服务帐户与 Google Analytics API 结合使用时。您需要确保在帐户级别添加权限。我不知道为什么它需要是帐户级别,我只知道它不能以任何其他方式正常工作。

关于Node.js:Google Analytics API:如何使用 "service account"发出授权的只读管理 API 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38123150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com