Google Analytics(分析)服务到服务google-auth-library-nodejs

时间:2018-08-30 15:16:06

标签: node.js google-analytics google-authentication

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

我通过JSON Web Tokens访问数据没有问题,我真的不想使用OAuth2,因为我一开始只能访问自己的帐户。下面列出的范围来自Query Explorer中的“ Google Analytics API,v3”。

鉴于错误消息,我尝试了其他范围的各种迭代,其中最著名的是“ Analytics Reporting API,v4”的迭代。
注意:Google Developers Console服务帐户密钥中提供的电子邮件已添加到Analytics(分析)管理控制台的所有权限(帐户,媒体资源,视图)。我还尝试了类似“ Google scopes”中所述的操作。

尝试使用“应用程序默认凭据”(在.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);

1 个答案:

答案 0 :(得分:0)

样例示例中指定的网址需要替换为分析查询,例如"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);