Google 目录 API 使用服务帐户 - 自签名证书错误

Google directory API using Service account - error self signed cert

正在尝试使用 Node.js 从 Google 目录用户 API 获取用户数据。已设置服务帐户并下载密钥 json 文件,并已为服务帐户授予适当的权限。 下面是代码。

'use strict';

const {google} = require('googleapis');
const path = require('path');

async function runSample() {
  // acquire an authentication client using a service account
  const auth = await google.auth.getClient({
    keyFile: ('./auth.json'),
    scopes: [
      'https://www.googleapis.com/auth/admin.directory.user.readonly',
    ],
  });

  // obtain the admin client
  const admin = google.admin({
    version: 'directory_v1',
    auth,
  });

  // Insert member in Google group
  const res = await admin.users.get({
    userKey: '{email}'
  });

  console.log(res.data);
}
runSample().catch(console.error);

出现此错误:

request to https://www.googleapis.com/oauth2/v4/token failed, reason: self signed certificate in certificate chain

我试过了:
-npm config set strict-ssl false
- 还尝试下载 ca-cert 和 npm 配置集 cafile "path/file.pem"

都没有帮助。 任何人都可以帮忙吗?

设置这个环境变量:

NODE_TLS_REJECT_UNAUTHORIZED=0

文档:

If value equals '0', certificate validation is disabled for TLS connections. This makes TLS, and HTTPS by extension, insecure. The use of this environment variable is strongly discouraged.

https://nodejs.org/api/cli.html#cli_node_tls_reject_unauthorized_value