[软件设计/软件工程] 可以使用 Google 的 node.js 客户端库生成 JWT 客户端

[复制链接]
发表于 2022-5-5 08:49:36
问题
我正在尝试通过 JWT 客户端使用 Google 的 node.js 客户端库生成访问令牌。

下面是我的代码片段:
  1. var google = require("googleapis");

  2. // Load the service account key JSON file.
  3. var serviceAccount = require("path/to/serviceAccountKey.json");

  4. // Specify the required scope.
  5. var scopes = [
  6.   "https://www.googleapis.com/auth/firebase"
  7. ];

  8. // Authenticate a JWT client with the service account.
  9. var jwtClient = new google.auth.JWT(
  10.   serviceAccount.client_email,
  11.   null,
  12.   serviceAccount.private_key,
  13.   scopes
  14. );

  15. // Use the JWT client to generate an access token.
  16. jwtClient.authorize(function(error, tokens) {
  17.   if (error) {
  18.     console.log("Error making request to generate access token:", error);
  19.   } else if (tokens.access_token === null) {
  20.     console.log("Provided service account does not have permission to generate access tokens");
  21.   } else {
  22.     var accessToken = tokens.access_token;

  23.     // Include the access token in the Authorization header.
  24.   }
  25. });
复制代码

但我不断收到此错误消息:

有谁知道是什么原因?

回答
从 26.0.0 node.js 客户端版本开始,该库似乎正在使用命名导入。当我更改代码时,代码工作正常

var google = require("googleapis");

到达

var {google} = 要求(“googleapis”);

所以这看起来像是 Firebase 中的一个文档错误。





上一篇:Flexbox 覆盖的网格难以或不可能实现的区域
下一篇:限制公网访问整个 WP 网站

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表