Google API Node js客户端语法错误

时间:2016-08-26 19:54:43

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

我尝试在快速设置中调用Google AnalyticsAPI。我在此级别遇到语法错误:key.'mail'。我从多个示例中构建了以下代码,但我无法找到可能出错的代码。

这是我的代码:

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

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

let jwtClient = new google.auth.JWT(
    key.'mail', 
    null,
    key.'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};

这是我的server.babel.js:

import express from 'express';

const app = express();

app.use('/', express.static('public'));
app.use('/', express.static('src'));

var authorRouter = require('./src/routes/authorRoutes');
var ga = require('./src/routes/garoute');

app.listen(process.env.PORT || 3000);

//templating engine
app.set('views', './src/views');      
app.set('view engine', 'ejs');

app.use('/test', ga.queryData());
app.use('/authors', authorRouter.getAuthors());

0 个答案:

没有答案