0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

okay, by using this function the token.json file will not expire?
By modifying authorize function the token.json file still expired and when I run my application I got a link for a new token which I have to give permission from Gmail.
I have updated my answer, check if it helps you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.