I'm trying to generate an access token via a JWT client using Google's node.js client library as mentioned here.
Here's the code snippet I'm following:
var google = require("googleapis");
// Load the service account key JSON file.
var serviceAccount = require("path/to/serviceAccountKey.json");
// Specify the required scope.
var scopes = [
"https://www.googleapis.com/auth/firebase"
];
// Authenticate a JWT client with the service account.
var jwtClient = new google.auth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
scopes
);
// Use the JWT client to generate an access token.
jwtClient.authorize(function(error, tokens) {
if (error) {
console.log("Error making request to generate access token:", error);
} else if (tokens.access_token === null) {
console.log("Provided service account does not have permission to generate access tokens");
} else {
var accessToken = tokens.access_token;
// Include the access token in the Authorization header.
}
});
But I keep getting this error message:
TypeError: Cannot read property 'JWT' of undefined
Anyone know what's causing this?
Looks like the library is using named imports as of the 26.0.0 node.js client release. The code works correctly when I change
var google = require("googleapis");
to
var {google} = require("googleapis");
So this looks to be a documentation error on Firebase's part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With