【问题标题】:Google directory API using Service account - error self signed cert使用服务帐户的 Google 目录 API - 错误自签名证书
【发布时间】:2019-07-12 11:24:32
【问题描述】:

尝试使用 Node.js 从 Google Directory User 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);

收到此错误:

https://www.googleapis.com/oauth2/v4/token的请求失败,原因:证书链中的自签名证书

我试过了:
- npm 配置设置严格 SSL 假
- 并且还尝试下载 ca-cert 和 npm config set cafile "path/file.pem"

两者都没有帮助。 有人可以帮忙吗?

【问题讨论】:

  • 您是否尝试过问题论坛,它似乎是图书馆的常见问题github.com/nodejs/node-gyp/issues/695
  • 设置此环境变量有效! NODE_TLS_REJECT_UNAUTHORIZED=0。感谢 DaImTo!
  • 发布您自己问题的答案,您可能会在未来帮助别人。

标签: node.js google-api google-api-nodejs-client google-directory-api


【解决方案1】:

设置这个环境变量:

NODE_TLS_REJECT_UNAUTHORIZED=0

文档:

如果 value 等于“0”,则为 TLS 连接禁用证书验证。 这使得 TLS 和扩展的 HTTPS 不安全。这个环境的使用 强烈建议不要使用变量。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-05
    • 2021-10-03
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2017-11-04
    相关资源
    最近更新 更多