You cannot increase the expiration time for access_code
.
However, you can generate new access token if the current access token expires!
- First get the
authentication code
.
- Get
refresh_token
from google
using authentication code.
- Get
access_token
using refresh_token
when
it expires.
I assume that you have the authentication code. If you do not have the code then here is an article on how you can integrate oauth to your react app and get the authentication code from google. You can check this out for more details.
Generating refresh and access token:
Replace the essential values (code
, client_id
, client_secret
) inside payload.
let payload = {
grant_type: 'authorization_code',
code: '****SFJSDFJKAN-DF',
client_id: '******.googleusercontent.com',
client_secret: 'GOCS*******m5Qzg',
redirect_uri: 'http://localhost:3000',
};
axios
.post(`https://oauth2.googleapis.com/token`, payload, {
headers: {
'Content-Type': 'application/json;',
},
})
.then((res: any) => {
return res.data;
})
.then((response: any) => {
console.log('refresh token: ', response);
})
.catch((err) => console.log('err: ', err));
You will get a response like this:
{
access_token: "********KAjJZmv4xLvbAIHey",
expires_in: 3599,
id_token: "***************VeM7cfmgbvVIg",
refresh_token: "***************VeM7cfmgbvVIg",
scope: "https://www.googleapis.com/auth/gmail.readonly openid
.....
.....
https://mail.google.com/",
token_type: "Bearer",
}
Save your refresh and access token.
Now you can use refresh token to generate new access token!
Generate new access token from refresh token.
let payloadForAccessToken = {
grant_type: 'refresh_token',
refresh_token: '*******SFKNSK***',
client_id: '******.googleusercontent.com',
client_secret: '*****cxCsrf***8UUm5Qzg',
};
axios
.post(`https://oauth2.googleapis.com/token`, payloadForAccessToken, {
headers: {
'Content-Type': 'application/json;',
},
})
.then((res: any) => {
return res.data;
})
.then((res) => {
console.log('new token response: ', res);
})
.catch((err) => console.log('err: ', err));