按照有关如何执行 G Suite 域范围授权的说明,请参见此处 https://developers.google.com/admin-sdk/directory/v1/guides/delegation 我完成了所有步骤,但现在我是坚持在我的应用程序中实现 SDK 目录服务对象,我在前端使用 Angular 7,后端使用 NodeJs(Firebase 云函数),但此处给出的代码仅支持 Java、Python 和 Go。谁能告诉我如何将其转换为 NodeJS?
小智 6
希望你现在已经有了答案,但是你去吧:
const keys = require('<Path to your privateKey.json file>');
const { JWT } = require('google-auth-library');
async function main() {
const client = new JWT({
email: keys.client_email,
key: keys.private_key,
subject: "<email address of user with admin acceess on G Suite>",
scopes: [<put your scopes here>],
});
const url = `<Your HTTP request>`;
const res = await client.request({ url });
return(res.data);
}
main().catch(console.error);
Run Code Online (Sandbox Code Playgroud)
这是您的代码外观的一个很好的示例:https : //github.com/googleapis/google-auth-library-nodejs/blob/master/samples/jwt.js
谷歌示例中唯一缺少的是主题参数。此参数是必须的,因为您尝试访问只有 G Suite 管理员才能访问的 API,因此您需要模拟管理员帐户。