简体   繁体   English

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

[英]Analytics service-to-service google-auth-library-nodejs

Referencing this doc JSON Web Tokens , I have tried to access the Analytics Reporting API, trying first "Application Default Credentials" then "JSON Web Tokens" authentication. 参考此文档JSON Web令牌 ,我尝试访问Analytics Reporting API,首先尝试“应用程序默认凭据”,然后尝试“ JSON Web令牌”身份验证。
Each time I receive the error Request had insufficient authentication scopes. 每次收到错误消息时, Request had insufficient authentication scopes.

I have no problem accessing the data through Query Explorer , and I really don't want to use OAuth2, since I would only ever be accessing my own account to begin with. 我没有通过Query Explorer访问数据的问题,并且我真的不想使用OAuth2,因为我一开始只能访问自己的帐户。 The scopes listed below were derived from "Google Analytics API, v3" in Google scopes . 下面列出的范围是从Google范围内的 “ Google Analytics API,v3”派生而来的。

Given the error message, I've tried various iterations of other scopes, most notably those for "Analytics Reporting API, v4." 考虑到错误消息,我尝试了其他范围的各种迭代,其中最著名的是“ Analytics Reporting API,v4”版本。
Note : the email provided in the service account key from the Google Developers Console has been added to all permissions (account, property, view) of the Analytics Admin Console. 注意 :Google Developers Console服务帐户密钥中提供的电子邮件已添加到Analytics(分析)管理控制台的所有权限(帐户,媒体资源,视图)。 I have also tried something similar as described in " Service <--> Service authentication ". 我还尝试了类似“ 服务<->服务身份验证 ”中所述的方法。

Attempting "Application Default Credentials" (setting path to service account key in .env): 尝试“应用程序默认凭据”(在.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);

Attempting "JSON Web Tokens:" 尝试“ 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);

The url specified in the boilerplate example needs to be replaced with the analytics query, such as that from "API Query URI" . 需要将样板示例中指定的url替换为分析查询,例如“ API Query URI”中的查询

For example, if I want number of page views, I would replace the url used in the question's snippet: 例如,如果我想要页面浏览量,我将替换问题代码段中使用的网址:

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

with that specified in the Query Explorer: 与查询资源管理器中指定的内容相同:

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

The resulting (functioning) code is as follows: 生成的(起作用的)代码如下:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cloud Foundry的服务到服务通信 - Service-to-service communication for Cloud Foundry 使用jwt调用google api以对原始端点进行服务到服务的调用的工作节点示例? - Working node example of calling google api with jwt for service-to-service call to raw endpoint? 使用 Google-Auth-Library 将服务帐户连接到 Admob API 时出现问题 - Problems connecting Service Account to Admob API with Google-Auth-Library 使用 nodejs 库为谷歌经销商 api 验证服务帐户时出现问题 - Problems authenticating Service Account for google reseller api using the nodejs library 使用Nodejs中的Google API进行服务身份验证 - Service Authentication with Google APIs in Nodejs Google API服务帐户和Node.js - Google API Service Account and nodejs 节点:通过服务帐户的Google Analytics(分析)数据 - Node: Google Analytics data via service account 无法验证NodeJS的Google Cloud Prediction API库中的服务帐户凭据 - Failed to authenticate Service Account credential in Google Cloud Prediction API Library for NodeJS 如何使用服务帐户凭据初始化 Google Cloud Storage NodeJS 客户端库 - How to init Google Cloud Storage NodeJS client library with service account credentials 如何在带有NodeJS的Google Cloud Storage中使用google-auth-library? - How to use google-auth-library with Google Cloud Storage with NodeJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM