Home  >  Q&A  >  body text

How to connect to Google Calendar API using service account using Node.js?

<p>How to use the Google Auth client when using Node.js. </p> <p>I checked many articles that the client URL will provide a code data that I can use to get the client details. However, how can I use a service client to automatically authenticate on behalf of the client. I mainly need to get the Google Meet link and add it to the client's Google calendar. </p> <p>I am following this code. But this is manual URL authentication. </p> <p>https://github.com/isuruhettiarachchi/ssd-oauth-assignment/blob/master/utils/google-util.js</p>
P粉921130067P粉921130067427 days ago491

reply all(1)I'll reply

  • P粉920835423

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

    Try something like the following, making sure domain-wide delegation is configured from your workspace account to the service account.

    subject is the user on the domain you want the service account to impersonate.

    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!");
      }
    });

    reply
    0
  • Cancelreply