gpt4 book ai didi

node.js - 谷歌分析 - invalid_grant : Invalid JWT Signature

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:19 27 4
gpt4 key购买 nike

我需要获得 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 中盗取

关于node.js - 谷歌分析 - invalid_grant : Invalid JWT Signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com