Alexander
Alexander

Reputation: 7832

Getting list of Google Calendar events using googleapis

Following googleapis documentation I've retrieved tokens including refresh_token:

{ access_token: 'ya29.Glv_LONG-STRING',
        token_type: 'Bearer',
        refresh_token: '1/7k6BLAH-BLAH-BLAH',
        expiry_date: 1532141656948 }

How can I get list of Google Calendar events using this when access_token is not valid anymore, but I have the refresh_token?

Upvotes: 1

Views: 156

Answers (1)

Tanaike
Tanaike

Reputation: 201388

You have already retrieved the refresh token. You want to retrieve an event list using Calendar API by the refresh token. If my understanding is correct, how about this sample script? In this script, it supposes the following points.

  • You have the refresh token which can use Calendar API.
    • The refresh token includes https://www.googleapis.com/auth/calendar or https://www.googleapis.com/auth/calendar.readonly for retrieving the event list using Calendar API in the scopes.
  • Calendar API has already been enabled at API console.

When you run the script, if the error occurs, please confirm above points.

Sample script :

const {google} = require('googleapis');
const calendar = google.calendar('v3');
var oauth2Client = new google.auth.OAuth2(
  "### client ID ###",
  "### client secret ###",
);
oauth2Client.setCredentials({
  refresh_token: "### refresh token ###",
  // access_token: "#####" // If you want to use access token, please use this.
});
calendar.events.list({
   auth: oauth2Client,
   calendarId: 'primary',
}, function(err, res) {
    if (err) {
        console.log(err);
    } else {
        console.log(res.data);
    }
});

Note :

  • When you use this sample script, please set client ID, client secret, refresh token and calendarId for your environment.
  • I confirmed that this sample script works with the version 32.0.0 of googleapis.

Reference :

If I misunderstand your question, I'm sorry.

Upvotes: 1

Related Questions