【问题标题】:google analytics api node.js谷歌分析 api node.js
【发布时间】:2016-10-26 00:55:35
【问题描述】:

我正在尝试在 node.js 中调用谷歌分析 api。谁能告诉我我做错了什么?我在 node_modules/mime/mime.js 中得到“path.replace 不是函数”,但我确定问题出在我的身份验证上。 (顺便说一句,我的日志没有被击中)

const
  gapi      = require ("googleapis"),
  profileid = '000000000',
  key       = require ('./key.json'),
  email     = 'email@email.com',
  scopes    = ['https://www.googleapis.com/auth/analytics', 'https://www.googleapis.com/auth/analytics.readonly'],
  jwt       = new gapi.auth.JWT (email, key, null, scopes, null);

gapi.analytics ('v3').data.ga.get ({
  'auth': jwt,
  'ids': 'ga:' + profileid,
  'start-date': '30daysAgo',
  'end-date': 'today',
  'metrics': 'ga:pageviews'
}, function (err, result) {
  console.log (err, result);
});

更新:我尝试像这样使用 API 密钥。在这种情况下,我的日志确实受到了打击。但我收到“需要登录”错误。

const
  gapi      = require ("googleapis"),
  API_KEY   = 'IzaSyDbIBVkt5kia3CJ3w2Y3-nsLHDpSruERkw';

gapi.analytics ('v3').data.ga.get ({
  'auth': API_KEY,
  'ids': 'ga:' + profileid,
  'start-date': '30daysAgo',
  'end-date': 'today',
  'metrics': 'ga:pageviews'
}, function (err, result) {
  console.log (err, results);
});

这个 oauth playground https://developers.google.com/oauthplayground/ 让我相信你需要一个谷歌分析登录和范围才能获得授权码。使用授权码,您可以获得访问令牌。一旦你有了它,你可以简单地用参数发出请求来得到你需要的东西。问题是在代码中得到它。

我看到的涉及获取授权代码的代码 sn-ps 以一种让用户从同意页面获取它的方式(比如这里 https://github.com/google/google-api-nodejs-client#retrieve-authorization-code),但我只是想做一个服务器到服务器应用程序,并且我没有获取用户数据,因此我不需要用户的许可。

【问题讨论】:

    标签: node.js google-analytics google-api jwt


    【解决方案1】:

    如果您正在执行服务器端请求,则可以使用 jwt 路由并使用 Google 在通过 cloud.google.com/console 上的“服务器端”流程时提供的 jwt json 文件。有关 jwt 的解释,请参阅 these docs 和有关 sn-p 的 this sample。它在示例中使用了 Google Drive API,但您的问题集中在让服务器端身份验证正常工作。

    我为使其工作所做的相关更改:

    var authClient = new google.auth.JWT(
    'service-account-email@developer.gserviceaccount.com',
    'CHANGE_THIS_TO_YOUR_JSON.json',
    // Contents of private_key.pem if you want to load the pem file yourself
    // (do not use the path parameter above if using this param)
    '',
    // Scopes can be specified either as an array or as a single, space-delimited string
    ['https://www.googleapis.com/auth/drive.readonly'],
    // User to impersonate (leave empty if no impersonation needed)
    '');
    

    【讨论】:

      【解决方案2】:

      我得到了第一个处理以下更改的方法。

      1. 翻转了 JWT 函数中的第二个和第三个参数
      2. 从 json 文件而不是整个文件中传入 private_key
      3. 使用 json 文件中的电子邮件而不是我的 google 电子邮件
      4. 用给定的电子邮件在谷歌分析中建立了新用户

        const
          gapi      = require ("googleapis"),
          profileid = '000000000',
          key       = require ('./key.json'),
          scopes    = 'https://www.googleapis.com/auth/analytics.readonly',
          jwt       = new gapi.auth.JWT (key.client_email, null, key.private_key, scopes);
        
        jwt.authorize (function (err, response) {
        
          gapi.analytics ('v3').data.ga.get ({
            'auth': jwt,
            'ids': 'ga:' + profileid,
            'start-date': '30daysAgo',
            'end-date': 'today',
            'metrics': 'ga:pageviews'
          }, function (err, result) {
            console.log (err, result);
          });
        
        });
        

      【讨论】:

        猜你喜欢
        • 2015-09-09
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 2018-09-13
        • 2016-05-11
        相关资源
        最近更新 更多