【发布时间】:2021-01-05 20:47:16
【问题描述】:
我正在尝试在firebase中添加功能以获取用户的分析数据。
code: 403,
> errors: [
> {
> message: 'User does not have any Google Analytics account.',
> domain: 'global',
> reason: 'forbidden'
> }
> ]
当我在命令提示符下运行它时出现此错误。不知道我在哪里做错了。我得到了访问令牌,但错误是由reports.batchGet 代码生成的。
import * as functions from 'firebase-functions';
const gClientEmail = functions.config().google.client_email
const gPrivateKey = functions.config().google.private_key.replace(/\\n/g, '\n')
const { google } = require('googleapis');
const jwtClient = new google.auth.JWT(gClientEmail, null, gPrivateKey, 'https://www.googleapis.com/auth/analytics.readonly', '');
const oauth2Client = new google.auth.OAuth2();
google.options({ auth: oauth2Client });
exports.init = functions.https.onRequest((request, response) => {
jwtClient.authorize((err:any, tokens:any) => {
if (err) {
throw (err);
}
oauth2Client.setCredentials({
access_token: tokens.access_token
});
var analytics = google.analyticsreporting('v4');
var req = {
reportRequests: [
{
viewId: VIEWID,
dateRanges: [
{
startDate: "10daysAgo",
endDate: "today",
},
],
metrics: [
{
expression: "ga:users",
},
],
dimensions: [
{
name: "ga:date",
},
],
}
],
};
analytics.reports.batchGet({
'auth': jwtClient,
'resource': req,
},
function(err:any, response:any) {
if (err) {
console.log(err);
return;
}
console.log(response);
}
);
});
});
我有一个解决方案,他们说在分析仪表板中将gClientEmail 添加到每个站点的用户。我不想这样做。
【问题讨论】:
标签: node.js firebase google-analytics google-cloud-functions service-accounts