How to connect to Google Calendar API using service account using Node.js?
P粉921130067
2023-08-17 23:52:59
<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>
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!"); } });