Skip to content

How to automatically refresh the access token? #21

Description

@shelomito12

My application has a status of 'Testing' and the consent screen is configured for an external user type, causing the token to expire in 7 days.

How to automatically refresh the access token on the next API call after it expires?

Currently, I'm manually generating the access token with the following code:

require('dotenv').config();
const readline = require('readline');
const {google} = require('googleapis');

function getToken() {
  const auth = new google.auth.OAuth2(
    process.env.CLIENT_ID,
    process.env.CLIENT_SECRET,
    'urn:ietf:wg:oauth:2.0:oob'
  );

  const authUrl = auth.generateAuthUrl({
    access_type: 'offline',
    scope: ['https://www.googleapis.com/auth/gmail.readonly'],
  });

  console.log('Authorize this app by visiting this url:');
  console.log(authUrl);

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  rl.question('Enter the code from that page here: ', (authCode) => {
    rl.close();
    auth.getToken(authCode, (err, token) => {
      if (err) {
        return console.log('Error retrieving access token', err);
      }

      console.log('Token:');
      console.log(token);
    });
  });
}

getToken();

Can you please advice? thanks

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions