Google 分析 - invalid_grant:JWT 签名无效

Google Analytics - invalid_grant: Invalid JWT Signature

我需要授权 Google analytics 来获取响应数据。

var google = require('googleapis'), 
  q = require('q'), 
  SERVICE_ACCOUNT_EMAIL = '838823084353-cjjoiv9di67fuh7geqgggociibataf9v@developer.gserviceaccount.com', 
  SERVICE_ACCOUNT_KEY_FILE = __dirname + '/google-services-private-key.pem';
  var def = q.defer(); 
  var gAnalytics = google.analytics('v3'); 
  var authClient = new google.auth.JWT( SERVICE_ACCOUNT_EMAIL, SERVICE_ACCOUNT_KEY_FILE, null, ['https://www.googleapis.com/auth/analytics.readonly']); 
  console.log(authClient) 

  authClient.authorize(function (err, tokens) { 
    if (err) { 
      console.log("err is: " + err, tokens); 
      return; 
    } 

但是授权失败

获取错误

JWT { transporter: DefaultTransporter {}, clientId_: undefined, clientSecret_: undefined, redirectUri_: undefined, opts: {},
credentials: { refresh_token: 'jwt-placeholder', expiry_date: 1 },
email: '838823084353-cjjoiv9di67fuh7geqgggociibataf9v@developer.gserviceaccount.com', keyFile: '/home/aaa/Desktop/ampretailer/server/google-services-private-key.pem', key: null, scopes: [ 'https://www.googleapis.com/auth/analytics.readonly' ], subject: undefined, gToken: [Function: GoogleToken] } err is: Error: invalid_grant: Invalid JWT Signature. { access_token: null, token_type: 'Bearer', expiry_date:null }

我建议您尝试使用 Google Analytics v4 而不是 v3 有许多您无法使用 V3 访问的维度和指标。

'use strict';

const { google } = require('googleapis');
const sampleClient = require('../sampleclient');

const analyticsreporting = google.analyticsreporting({
  version: 'v4',
  auth: sampleClient.oAuth2Client
});

async function runSample () {
  const res = await analyticsreporting.reports.batchGet({
    resource: {
      reportRequests: [{
        viewId: '65704806',
        dateRanges: [
          {
            startDate: '2018-03-17',
            endDate: '2018-03-24'
          }, {
            startDate: '14daysAgo',
            endDate: '7daysAgo'
          }
        ],
        metrics: [
          {
            expression: 'ga:users'
          }
        ]
      }]
    }
  });
  console.log(res.data);
  return res.data;
}

// if invoked directly (not tests), authenticate and run the samples
if (module === require.main) {
  const scopes = ['https://www.googleapis.com/auth/analytics'];
  sampleClient.authenticate(scopes)
    .then(c => runSample())
    .catch(e => console.error);
}

// export functions for testing purposes
module.exports = {
  runSample,
  client: sampleClient.oAuth2Client
};

analyticsReporting/batchGet.js

中窃取的代码

Service account - To use the service account based samples, create a new service account in the cloud developer console, and save the file as jwt.keys.json in the samples directory.

'use strict';

const {google} = require('googleapis');
const path = require('path');

/**
 * The JWT authorization is ideal for performing server-to-server
 * communication without asking for user consent.
 *
 * Suggested reading for Admin SDK users using service accounts:
 * https://developers.google.com/admin-sdk/directory/v1/guides/delegation
 *
 * See the defaultauth.js sample for an alternate way of fetching compute credentials.
 */
async function runSample () {
  // Create a new JWT client using the key file downloaded from the Google Developer Console
  const client = await google.auth.getClient({
    keyFile: path.join(__dirname, 'jwt.keys.json'),
    scopes: 'https://www.googleapis.com/auth/analytics.readonly'
  });

  // Obtain a new drive client, making sure you pass along the auth client
  const analyticsreporting = google.analyticsreporting({
    version: 'v4',
    auth: client
  });

  // Make an authorized request to list Drive files.
  const res = = await analyticsreporting.reports.batchGet({
    resource: {
      reportRequests: [{
        viewId: '65704806',
        dateRanges: [
          {
            startDate: '2018-03-17',
            endDate: '2018-03-24'
          }, {
            startDate: '14daysAgo',
            endDate: '7daysAgo'
          }
        ],
        metrics: [
          {
            expression: 'ga:users'
          }
        ]
      }]
    }
  });
  console.log(res.data);

  return res.data;
}

if (module === require.main) {
  runSample().catch(console.error);
}

// Exports for unit testing purposes
module.exports = { runSample };

samples/jwt.js

中窃取的代码