so I have a nodejs application that sends invites to an event in google calendar to some people, for now I am using my gmail account and OAuth Playground to get a temporary Access token and it works, but the access token is just available for minutes and each time I need to refresh the Access token manually and give access to my google account calendar and this is the problem, now I want to make something dynamic without me interfering in the process. This application is hosted in wix. any suggestions ? Thanks
Asked
Active
Viewed 1,173 times
2 Answers
2
IF you are only letting them access a calendar that you own and control then you can use a service account.
let google = require('googleapis');
let privatekey = require("./privatekey.json");
Now let’s use the private key to configure a JWT auth client and authenticate our request.
// configure a JWT auth client
let jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
['https://www.googleapis.com/auth/calendar']);
//authenticate request
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log("Successfully connected!");
}
});
Create service account credentials in google developer console. then take the service account email address and share the calendar with it via the google calendar website. The rest of the code you have should be the same just swap out the auth section with this.

Linda Lawton - DaImTo
- 106,405
- 32
- 180
- 449
-
First, thanks for your response and time,how i can get the privatekey ? – Dah Mar 30 '20 at 11:49
-
https://console.developers.google.com/ create a new project an create service account credentials make sure to enable the calendar api. – Linda Lawton - DaImTo Mar 30 '20 at 11:52
-
1https://github.com/googleapis/google-api-nodejs-client <-- you can also check the readme under service account authencation. – Linda Lawton - DaImTo Mar 30 '20 at 12:11
-
Error: Service accounts cannot invite attendees without Domain-Wide Delegation of Authority. – Dah Mar 30 '20 at 13:04
1
To access the calendar API please follow the Quickstart
The code provided creates a refresh token that will automatically generate a new access token for you, whenever the old one expires.
Be careful with unnecessary using service accounts, especially for adding invitees to a calendar event - there are currently issues with this feature.

ziganotschka
- 25,866
- 2
- 16
- 33
-
First, thanks for your response and time, but my first work builds on this Quickstart.the problem here just like I said it asked for a user to give permission manually. – Dah Mar 30 '20 at 12:53
-
1Yes, asking the user to give the permission manually is intended behavior of OAuth2 for security reasons. If this is an issue, you might be interested in [offline access](https://developers.google.com/identity/protocols/oauth2/web-server#offline). In this case the user needs to give manual permission only once.[More](https://stackoverflow.com/a/60815451/11599789) to this topic. – ziganotschka Mar 30 '20 at 13:01