首页  >  问答  >  正文

如何使用Node.js使用服务帐号连接到Google日历API?

<p>如何在使用Node.js时使用Google Auth客户端。</p> <p>我查阅了许多文章,客户端URL将提供一个代码数据,我可以使用它来获取客户端详细信息。但是,我如何使用服务客户端代表客户自动进行身份验证。我主要需要获取Google Meet链接并添加到客户的Google日历中。</p> <p>我正在遵循这段代码。但这是手动URL身份验证。</p> <p>https://github.com/isuruhettiarachchi/ssd-oauth-assignment/blob/master/utils/google-util.js</p>
P粉921130067P粉921130067427 天前495

全部回复(1)我来回复

  • P粉920835423

    P粉9208354232023-08-18 19:36:09

    尝试类似以下的操作,确保从您的工作区帐户向服务帐户配置域范围委派。

    subject是您希望服务帐户模拟的域上的用户。

    let google = require('googleapis');
    let privateKey = require("./privatekey.json");
    
    var jwtClient = new google.auth.JWT({
           email: privateKey.client_email,
           key: privateKey.private_key,
           scopes: ['https://www.googleapis.com/auth/calendar'],
           subject: 'user@domain.com'
        });
    
    jwtClient.authorize(function (error, tokens) {
      if (error) {
        console.log(error);
        return;
      } 
      else {
        console.log("Successfully connected!");
      }
    });

    回复
    0
  • 取消回复