【发布时间】: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