【问题标题】:Instantiate an Admin SDK Directory service object with NodeJS使用 NodeJS 实例化一个 Admin SDK 目录服务对象
【发布时间】:2019-06-17 17:55:04
【问题描述】:

按照有关如何执行 G Suite 域范围内授权的说明进行操作,请参见此处 https://developers.google.com/admin-sdk/directory/v1/guides/delegation 我完成了所有步骤,但现在我坚持在我的应用程序中实现 SDK 目录服务对象,我在前端使用 Angular 7,在后端使用 NodeJs(Firebase 云函数),但此处给出的代码仅支持 Java, Python 和 Go。谁能告诉我如何将其转换为 NodeJS?

【问题讨论】:

    标签: node.js angular


    【解决方案1】:

    希望你现在已经找到了答案,但现在你去吧:

    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);
    

    这是一个很好的例子来说明你的代码应该是什么样子:https://github.com/googleapis/google-auth-library-nodejs/blob/master/samples/jwt.js

    google 的示例中唯一缺少的是主题参数。当您尝试访问只有 G Suite 管理员可以访问的 API 时,此参数是必须的,因此您需要模拟管理员帐户。

    【讨论】:

    • 这救了我的命,但有没有办法用 googleapis 库做到这一点?
    • 在@DavidKamer下方查看我的答案
    【解决方案2】:

    kamilestu 答案的续集

    这适用于谷歌 api

    const client = new JWT({
      email: privatekey.client_email,
      key: privatekey.private_key,
      subject: "<email address of user with admin acceess on G Suite>",
      scopes: SCOPES,
    });
    
    async function getUser(email) {
      try {
        const admin = await google.admin({
          version: "directory_v1",
          auth: client,
        });
        //get all users
        const users = await admin.users.get({
          userKey: email,
        });
        console.log(users, "users");
      } catch (error) {
        console.log(
          error.response ? error.response.data : error.message,
          "error",
          error.message ? error.errors : ""
        );
      }
    }
    
    getUser("<email address of user with admin acceess on G Suite>");
    

    【讨论】:

      猜你喜欢
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多