Saichandhra Arvapally
Saichandhra Arvapally

Reputation: 111

How to create a new token.json file using gmail api?

I'm working on Gmail API. I have a token.json file, in which I have an access token and refresh token. but there is an expiry for those values. When It expires I need to run the application and they will provide me a link to need permission from my Gmail account. and after giving permission they will provide a key which we have to place in our terminal for getting a new token. But how can I create this new token file without doing this manually? Is there any way to create this token.json file dynamically?

Upvotes: 0

Views: 3295

Answers (1)

fullfine
fullfine

Reputation: 1461

I understand that you have run the quickstart sample of the Gmail API and you want to create a new token.json without waiting the expiration time. In order to achieve this, you can modifiy the function authorize in the following way:

function authorize(credentials, callback) {
  const {client_secret, client_id, redirect_uris} = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
      client_id, client_secret, redirect_uris[0]);

  return getNewToken(oAuth2Client, callback);

}

update

The token.json will expire. I understood that you wanted to generate a new one in each run. If you want it not to expire, I recommend you read the notes of the Quickstart.

The authorization flow in this example is designed for a command line application. For information on how to perform authorization in other contexts, see the Authorizing and Authenticating. section of the library's README.

Here you can find different ways to authenticate Google APIs, which meet your requirement. I recommend you read OAuth2 and Service account credentials.

Upvotes: 1

Related Questions