简体   繁体   English

无法使用 Google 的 node.js 客户端库生成 JWT 客户端

[英]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.

相关问题 无法使用 Google 的 node.js 客户端库生成 JWT 客户端。 运行代码时出现 Typeerror - Can't generate JWT client with Google's node.js client library. Getting Typeerror while running the code 我无法以编程方式使用 node.js 客户端库从 google-cloud-automl 获取 model id - I can't programmatically get the model id from google-cloud-automl with node.js client library 上下文IO node.js客户端库 - Context IO node.js client library 为client和node.js发布库 - Publish a library for client and node.js 如何在Node.js应用程序中的客户端站点上处理JWT令牌? - How to handle JWT token on the client site in Node.js application? 如何将 jwt 从 swift 客户端传递到 node.js 服务器 - how to pass jwt from swift client to node.js server Node.js:获取客户端的 IP - Node.js: Get client's IP 如何使用 node.js 客户端库以编程方式从 google-cloud-automl 获取模型 ID - How to programmatically get model id from google-cloud-automl with node.js client library 使用TypeScript时,扩展Node.js客户端库中的“ conv”实例以在Google v2上执行操作 - Extend “conv” instance in Node.js Client Library for Actions on Google v2 when using TypeScript 在node.js中生成JavaScript以便在客户端使用的方法 - Ways to generate javascript in node.js for use on the client side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM