无法使用节点和 GA 分析 api 读取 属性 'join' 的空错误

Cannot read property 'join' of null error with node and GA analytics api

我正尝试按照本教程 http://www.2ality.com/2015/10/google-analytics-api.html 使用 Node.js 中的 Google Analytics Core Reporting API。我使用 babel-node mygaapiscript.js 直接从我的终端启动脚本。但出现以下错误:

  /Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:160
        this.scope = options.scope.join(' ');
                                  ^

    TypeError: Cannot read property 'join' of null
        at GoogleToken._configure (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:160:31)
        at new GoogleToken (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:19:8)
        at JWT.GoogleToken [as gToken] (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/gtoken/lib/index.js:17:12)
        at JWT._createGToken (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/google-auth-library/lib/auth/jwtclient.js:218:24)
        at JWT.refreshToken_ (/Users/me/Desktop/dashboard/dashboard-ga/node_modules/google-auth-library/lib/auth/jwtclient.js:133:15)
        at JWT.authorize (/Users/me/Desktop/dashboard/me-MacBook-Pro:routes

这个错误是什么意思?

这是我的脚本:

#!/usr/bin/env babel-node
import google from 'googleapis';
'use strict';

import key from './client_id.json';
const VIEW_ID = 'ga:70810323';

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

    jwtClient.authorize(function (err, tokens) {
      if (err) {
        console.log(err);
        return;
      }
      let analytics = google.analytics('v3');
      queryData(analytics);
    });

    function queryData(analytics) {
      analytics.data.ga.get({
        'auth': jwtClient,
        'ids': VIEW_ID,
        'metrics': 'ga:uniquePageviews',
        'dimensions': 'ga:pagePath',
        'start-date': '30daysAgo',
        'end-date': 'yesterday',
        'sort': '-ga:uniquePageviews',
        'max-results': 10,
      }, function (err, response) {
        if (err) {
          console.log(err);
          return;
        }
        console.log(JSON.stringify(response, null, 4));
      });  
    }

module.exports = {queryData};

得到 module.exports 因为我在 express 中使用它。

您对 google.auth.JWT() 的论点似乎是错误的。请参阅 the documentation and the JWT example in the googleapis repo here 中的示例。如果您没有为第二个参数传递密钥文件名,则第三个参数应该是文字密钥数据。所以只需为第三个参数添加 null

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