简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM