分析服务到服务google-auth-library-nodejs

问题描述 投票:0回答:1

参考此文档JSON Web Tokens,我尝试访问Analytics Reporting API,首先尝试“应用程序默认凭据”,然后尝试“JSON Web令牌”身份验证。 每次我收到错误Request had insufficient authentication scopes.

我通过Query Explorer访问数据没有问题,我真的不想使用OAuth2,因为我只会开始访问自己的帐户。下面列出的范围源自Google scopes中的“Google AnalyticsAPI,v3”。

鉴于错误消息,我尝试了其他范围的各种迭代,最值得注意的是“Analytics Reporting API,v4”。 注意:Google Developers Console中服务帐户密钥中提供的电子邮件已添加到Analytics管理控制台的所有权限(帐户,属性,视图)中。我也尝试过类似“Service <--> Service authentication”中描述的内容。

尝试“应用程序默认凭据”(在.env中设置服务帐户密钥的路径):

const {auth} = require('google-auth-library');
async function main() {
  const client = await auth.getClient({
    scopes: [
      'https://www.googleapis.com/auth/analytics',
      'https://www.googleapis.com/auth/analytics.edit',
      'https://www.googleapis.com/auth/analytics.manage.users',
      'https://www.googleapis.com/auth/analytics.manage.users.readonly',
      'https://www.googleapis.com/auth/analytics.provision',
      'https://www.googleapis.com/auth/analytics.readonly',
      'https://www.googleapis.com/auth/analytics.user.deletion'
    ]
  });
  const projectId = await auth.getProjectId();
  const url = `https://www.googleapis.com/dns/v1/projects/${projectId}`;
  const res = await client.request({ url });
  console.log(res.data);
}
main().catch(console.error);

尝试“JSON Web令牌:”

const {JWT} = require('google-auth-library');
const keys = require('../service-account-credentials.json');

async function main() {
  console.log('keys.client_email: ', keys.client_email)
  console.log('keys.private_key: ', keys.private_key)
  const client = new JWT(
    keys.client_email,
    null,
    keys.private_key,
    [
    'https://www.googleapis.com/auth/analytics',
    'https://www.googleapis.com/auth/analytics.edit',
    'https://www.googleapis.com/auth/analytics.manage.users',
    'https://www.googleapis.com/auth/analytics.manage.users.readonly',
    'https://www.googleapis.com/auth/analytics.provision',
    'https://www.googleapis.com/auth/analytics.readonly',
    'https://www.googleapis.com/auth/analytics.user.deletion'
    ],
  );
  const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
  const res = await client.request({url});
  console.log(res.data);
  const tokenInfo = await client.getTokenInfo(client.credentials.access_token);
  console.log(tokenInfo);
}

main().catch(console.error);
node.js google-analytics google-authentication
1个回答
0
投票

样板示例中指定的url需要替换为分析查询,例如来自"API Query URI"的分析。

例如,如果我想要多少页面浏览量,我会替换问题代码段中使用的网址:

https://www.googleapis.com/dns/v1/projects/${projectId}

使用查询资源管理器中指定的:

https://www.googleapis.com/analytics/v3/data/ga?ids=ga%${analyticsViewOrProfileId}&start-date=30daysAgo&end-date=2018-08-28&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews

生成的(正常运行的)代码如下:

const {auth} = require('google-auth-library');

async function main() {
  const client = await auth.getClient({
    scopes: [
      'https://www.googleapis.com/auth/analytics',
      'https://www.googleapis.com/auth/analytics.readonly'
    ]
  });
  const viewId = <VIEW ID FROM ANALYTICS CONSOLE>
  const url = `https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A${viewId}&start-date=30daysAgo&end-date=2018-08-28&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews`;
  const res = await client.request({ url });
  console.log(res.data);
}
main().catch(console.error);
© www.soinside.com 2019 - 2024. All rights reserved.