[英]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 .我正在尝试使用此处提到的 Google 的node.js 客户端库通过 JWT 客户端生成访问令牌。
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
类型错误:无法读取未定义的属性“JWT”
Anyone know what's causing this?有谁知道这是什么原因造成的?
Looks like the library is using named imports as of the 26.0.0 node.js client release.从26.0.0 node.js 客户端版本开始,该库似乎正在使用命名导入。 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.所以这看起来是 Firebase 的文档错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.