【问题标题】:How to authenticate to Google Analytics Reporting API v4如何向 Google Analytics Reporting API v4 进行身份验证
【发布时间】:2016-06-13 10:46:33
【问题描述】:

我在使用Node.js client library 向Google Analytics Reporting API v4 进行身份验证时遇到了麻烦。 我尝试了所有方法,从 JWT(服务令牌:JSON 和 P12)、API 密钥和 OAuth 2.0 开始,但我从未成功通过身份验证。

我在我的开发者控制台中激活了 API,创建了 ID 并授予了我的谷歌分析属性和视图的权限。 我成功获得了我的服务帐户的授权和访问令牌,但我不知道如何使用它来向 Analytics Reporting API v4 进行身份验证。

我遇到了 401 错误消息:“请求没有有效的身份验证凭据”。 我尝试使用 JWT 模拟用户,但服务帐户未经授权。

使用 Node.js 客户端库和 JWT 身份验证:

var google = require('googleapis.js');

var viewID = 'XXXXXXXXX'; // Google Analytics view ID
var key = require('service_account.json'); // Service account

var jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, ['https://www.googleapis.com/auth/analytics.readonly'], null);
var oauth2Client = new google.auth.OAuth2();

jwtClient.authorize(function(err, result) {
  if (err) {
    console.log('Unauthorize');
    console.log(err);
    return;
  }

  oauth2Client.setCredentials({
    access_token: result.access_token
  });

  //Need to authenticate somewhere near here
  var analytics = google.analyticsreporting('v4');
  //Or here

  var req = {
    reportRequests: [
      {
        viewId: viewID,
        dateRanges: [
          {
            startDate: '2016-05-01',
            endDate: '2016-06-30',
          },],
        metrics: [
          {
            expression: 'ga:users',
          }, {
            expression: 'ga:sessions',
          },],
      },],
  };

  //Maybe here
  analytics.reports.batchGet(req,
    function(err, response) {
      if (err) {
        console.log('Fail');
        console.log(err);
        return;
      }
      console.log('Success');
      console.log(response);
    }
  );
});

Node.js 客户端库的先前版本似乎有一种指定客户端的方法,但它消失了,可能已被弃用。

withAuthClient(oauth2Client)

我尝试在 API 调用或请求中传递客户端或令牌,但没有任何效果。

google.analyticsreporting({ version: 'v4', auth: oauth2Client });
google.analyticsreporting({ version: 'v4', access_token: result.access_token });

也许这是一个菜鸟问题但我不知道该怎么做.

如果有人成功通过 Analytics Reporting API v4 的身份验证,请帮助:/

【问题讨论】:

  • 所以 GA API v4 也适用于 Node.js?我以为它还在 alpha 版本上?

标签: node.js api authentication google-analytics


【解决方案1】:

找出我缺少的东西:

  • Google API 客户端库“选项”:

    google.options({ auth: oauth2Client }); //this one is not very optional
    
  • 与 Google Analytics Reporting API v4 文档不同,使用客户端库的查询必须有标头来为每个请求指定客户端(感谢 CVarisco 注意到,client library documentation 并不准确..):

    var request ={
        'headers': {'Content-Type': 'application/json'},
        'auth': oauth2Client,
        'resource': req,
    };
    

【讨论】:

【解决方案2】:

另请注意,在服务器端使用服务帐户进行身份验证的推荐方法是使用 auth.getApplicationDefault。

https://developers.google.com/identity/protocols/application-default-credentials

我们建议在任何 以下情况:
...剪辑...
- 您正在使用与云项目相关联的数据或以其他方式限定整个应用程序而不是个人用户数据的数据访问 API。对于涉及用户数据的调用,最好使用最终用户明确同意访问的授权流程(请参阅使用 OAuth 2.0 访问 Google API)。

【讨论】:

    【解决方案3】:

    这就是我能够做到的:

    您需要在此处为​​您的应用程序创建凭据:https://console.developers.google.com/apis。这将为您提供客户端 ID 和客户端密码(注意 - 请确保 https://developers.google.com/oauthplayground 作为授权的重定向 uri)。然后转到https://developers.google.com/oauthplayground 创建授权令牌。您获得的访问令牌应通过标头和值发送到 Google API:“Authorization”:“Bearer {{AccessToken}}”。如果您的令牌过期,您可以通过点击 OAUTH api (https://www.googleapis.com/oauth2/v4/token)、在查询中发送 refresh_token、client_id、client_secret、grant_type 和 access_type 并在 'user-agent': 'google-oauth-playground' 中检索一个新令牌标题。

    如果您遇到困难,请按照他们在此处描述的方式实现 google API:https://developers.google.com/analytics/devguides/reporting/core/v4/quickstart/web-js。然后,您可以在浏览器中使用开发人员工具来准确查看将哪些值发送到何处。

    希望这会有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多