Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't generate JWT client with Google's node.js client library

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?

like image 216
not_a_bot Avatar asked Apr 27 '18 18:04

not_a_bot


1 Answers

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.

like image 54
not_a_bot Avatar answered Oct 20 '22 16:10

not_a_bot