Google 目录 API:无法使用服务帐户访问 User/Group 端点 (403)
Google Directory API: Unable to access User/Group endpoints using Service Account (403)
我正在尝试使用 Google 目录 API 验证群组成员,但每次发出请求时都无法通过 403 错误。
我使用的是服务帐户,我已为其启用了 "Enable G Suite Domain-wide Delegation" 选项。我还在 "Manage API Client Access"
下使用套件中的客户端 ID 添加了“https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group”范围
在代码方面,我为此使用了 Node,并且 google 提供了来自 NPM 的 googleapis 包。
外部JSON文件是我创建服务用户时下载的JSON凭证文件。
这是我尝试获取请求的代码。
import { google } from 'googleapis';
async function getGroupUsers(){
const auth = await google.auth.getClient({
keyFile: './src/jwt.keys.json',
scopes: [
'https://www.googleapis.com/auth/admin.directory.group',
'https://www.googleapis.com/auth/admin.directory.group.member',
],
});
const admin = google.admin({
version: 'directory_v1',
auth,
});
const res = await admin.groups.get({
groupKey: 'redacted@domain.redacted',
});
console.log(res)
}
我看不出这不起作用的任何明显原因,因为我看不出用户为何没有资源权限?
显然这里遗漏了一些明显的东西,因为 google 这方面的文档令人遗憾地遍布整个商店。
帮助不胜感激!
谢谢
加雷思
好吧,经过大量的头脑和谷歌搜索,我终于有了这个,最终的工作代码如下,不包括 client.subject 值,它必须是相关域的管理员.
async function validateToken(idToken) {
const keys = JSON.parse(GOOGLE_CREDS);
const client = auth.fromJSON(keys);
client.scopes = [
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.group',
];
client.subject = 'admin@gsuite.domain';
const admin = google.admin({
version: 'directory_v1',
// auth,
auth: client,
});
const res = await admin.groups.list({
domain: 'redacted',
userKey: email,
});
const { groups } = res.data;
let role = '';
// Check for user role
if (containsGroup(USER_GROUP, groups)) {
role = USER_GROUP;
}
// Check for admin role
if (containsGroup(ADMIN_GROUP, groups)) {
role = ADMIN_GROUP;
}
// Not an admin or user so return unathenticated
if (role === '') {
return authResponse();
}
return successResponse({
'X-Hasura-User-Id': userid,
'X-Hasura-Email': email,
'X-Hasura-Role': role,
'X-Hasura-Groups': groups.map(group => group.id),
'Cache-Control': 'max-age=600',
});
}
我正在尝试使用 Google 目录 API 验证群组成员,但每次发出请求时都无法通过 403 错误。
我使用的是服务帐户,我已为其启用了 "Enable G Suite Domain-wide Delegation" 选项。我还在 "Manage API Client Access"
下使用套件中的客户端 ID 添加了“https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group”范围在代码方面,我为此使用了 Node,并且 google 提供了来自 NPM 的 googleapis 包。
外部JSON文件是我创建服务用户时下载的JSON凭证文件。
这是我尝试获取请求的代码。
import { google } from 'googleapis';
async function getGroupUsers(){
const auth = await google.auth.getClient({
keyFile: './src/jwt.keys.json',
scopes: [
'https://www.googleapis.com/auth/admin.directory.group',
'https://www.googleapis.com/auth/admin.directory.group.member',
],
});
const admin = google.admin({
version: 'directory_v1',
auth,
});
const res = await admin.groups.get({
groupKey: 'redacted@domain.redacted',
});
console.log(res)
}
我看不出这不起作用的任何明显原因,因为我看不出用户为何没有资源权限?
显然这里遗漏了一些明显的东西,因为 google 这方面的文档令人遗憾地遍布整个商店。
帮助不胜感激!
谢谢
加雷思
好吧,经过大量的头脑和谷歌搜索,我终于有了这个,最终的工作代码如下,不包括 client.subject 值,它必须是相关域的管理员.
async function validateToken(idToken) {
const keys = JSON.parse(GOOGLE_CREDS);
const client = auth.fromJSON(keys);
client.scopes = [
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.group',
];
client.subject = 'admin@gsuite.domain';
const admin = google.admin({
version: 'directory_v1',
// auth,
auth: client,
});
const res = await admin.groups.list({
domain: 'redacted',
userKey: email,
});
const { groups } = res.data;
let role = '';
// Check for user role
if (containsGroup(USER_GROUP, groups)) {
role = USER_GROUP;
}
// Check for admin role
if (containsGroup(ADMIN_GROUP, groups)) {
role = ADMIN_GROUP;
}
// Not an admin or user so return unathenticated
if (role === '') {
return authResponse();
}
return successResponse({
'X-Hasura-User-Id': userid,
'X-Hasura-Email': email,
'X-Hasura-Role': role,
'X-Hasura-Groups': groups.map(group => group.id),
'Cache-Control': 'max-age=600',
});
}