gmail-tester icon indicating copy to clipboard operation
gmail-tester copied to clipboard

Token expire after 7 days

Open muhammadmubeen12345 opened this issue 3 years ago • 23 comments

How to automatically update the token in gmail API.

muhammadmubeen12345 avatar Apr 03 '21 06:04 muhammadmubeen12345

I'm with the same issue, of course I know about the function refresh-token that @levz0r created, but even using that function returns me an error "invalid_grant", if you could give us a complete example on how to use refresh-token would be great @levz0r

thank you in advanced

damasofc avatar Apr 05 '21 15:04 damasofc

I have the same problem and API for the refresh token doesn't work

pakhilov avatar May 11 '21 17:05 pakhilov

Yeah I have the same problem. Not sure how long it's lasting exactly but the refresh_access_token function is not working.

LordA98 avatar Jun 09 '21 21:06 LordA98

@muhammadmubeen12345 @damasofc @pakhilov

Did any of you find a solution / workaround in the end?

LordA98 avatar Jun 12 '21 10:06 LordA98

@LordA98 I have not found any solution about update of token automatically. Every Monday I update the token manually...

muhammadmubeen12345 avatar Jun 12 '21 10:06 muhammadmubeen12345

Hey @LordA98 and @muhammadmubeen12345 have you both tried "refresh_access_token"?

I just added to my cypress project easily with this file: refreshGmailToken.ts.

image

I then call this script on each CI run uploading the new token to S3 here: github workflow!

image

This will be downloaded by the following runs!

I hope I was able to help you and if you need more help just let me know! If I helped you can star my repository here where I test all sort of stuff with cypress, gmail and more: https://github.com/riccardogiorato/cypress-for-everything

riccardogiorato avatar Aug 29 '21 09:08 riccardogiorato

Hey @LordA98 and @muhammadmubeen12345 have you both tried "refresh_access_token"?

I just added to my cypress project easily with this file: refreshGmailToken.ts.

image

I then call this script on each CI run uploading the new token to S3 here: github workflow!

image

This will be downloaded by the following runs!

I hope I was able to help you and if you need more help just let me know! If I helped you can star my repository here where I test all sort of stuff with cypress, gmail and more: https://github.com/riccardogiorato/cypress-for-everything

Yes, even after using the refresh_access_token function, the 'invalid_grant' error will eventually show up.

the-bmc-dev avatar Nov 19 '21 15:11 the-bmc-dev

@the-bmc-dev can it be because you are scanning the inbox too frequently of using broad filters?

levz0r avatar Nov 22 '21 21:11 levz0r

@levz0r I did not think about that, are there such restrictions on Gmail API? Edit: I've checked the quotas and none of them were reached.

the-bmc-dev avatar Nov 23 '21 08:11 the-bmc-dev

Hi, I created this in plugins/index.js on("task", { "gmail:refresh_access_token": async () => { const token = await gmail_tester.refresh_access_token( path.resolve(__dirname, "credentials.json"), path.resolve(__dirname, "token.json") ); console.log("Refreshed the gmail token!"); return token; }, });

Screenshot 2021-12-21 at 12 00 51

and I call it like this in my test: cy.task("gmail:refresh_access_token");

I am getting this error:

`cy.task('gmail:refresh_access_token') failed with the following error:

> No refresh token is set.`

Any idea where do I make mistake?

omersomuncu avatar Dec 21 '21 10:12 omersomuncu

Hi, I created this in plugins/index.js on("task", { "gmail:refresh_access_token": async () => { const token = await gmail_tester.refresh_access_token( path.resolve(__dirname, "credentials.json"), path.resolve(__dirname, "token.json") ); console.log("Refreshed the gmail token!"); return token; }, });

Screenshot 2021-12-21 at 12 00 51

and I call it like this in my test: cy.task("gmail:refresh_access_token");

I am getting this error:

`cy.task('gmail:refresh_access_token') failed with the following error:

> No refresh token is set.`

Any idea where do I make mistake?

Thanks for the code example. I too would love to hear how others are using it, otherwise without a fully working refresh token they expire every two days so the whole plugin becomes not very useful.

dkoudrinhc avatar Dec 22 '21 02:12 dkoudrinhc

Hey folks,

I find the solution.

First of all, do not forget to add "refresh_token":"Your token" to your token.json!!! After that index.js: Screenshot 2021-12-28 at 13 04 04

It's ready to test: cy.task("gmail:refresh_access_token", {});

Have fun!

omersomuncu avatar Dec 28 '21 12:12 omersomuncu

Hey folks,

I find the solution.

First of all, do not forget to add "refresh_token":"Your token" to your token.json!!! After that index.js: Screenshot 2021-12-28 at 13 04 04

It's ready to test: cy.task("gmail:refresh_access_token", {});

Have fun!

Unfortunately this didn't seem to help at all, it just says 'invalid_grant' when running the refresh_token task now. e.g. CypressError: cy.task('gmail:refresh_access_token') failed with the following error:

invalid_grant

dkoudrinhc avatar Jan 20 '22 01:01 dkoudrinhc

Hi, I'm using the refress_access_token function but it requires to open a url and the copy again the code. Is there any way to avoid this?

josego1984 avatar Jul 14 '22 16:07 josego1984

Hey folks,

I find the solution.

First of all, do not forget to add "refresh_token":"Your token" to your token.json!!! After that index.js: Screenshot 2021-12-28 at 13 04 04

It's ready to test: cy.task("gmail:refresh_access_token", {});

Have fun!

@omersomuncu Check this issue. The key refresh_token should be set, check your applicationtype-setting for the client -> should be Desktop app

digitalgopnik avatar Feb 27 '23 09:02 digitalgopnik

@levz0r could you please help to resolve this issue?

taninnazar avatar Jul 18 '23 11:07 taninnazar

would be nice to have this done automatically also, the above solutions only work if you save token.json file to disk

Revadike avatar Jul 26 '23 08:07 Revadike

Hi @omersomuncu im trying to solution i don't have a CI/CD pipeline. I am just executing the test from terminal. How can i automatically refresh the token? Please can anyone support ? @Revadike @taninnazar @levz0r

mariamaslam avatar Nov 16 '23 05:11 mariamaslam

@mariamaslam there is nothing to do with a code. To have a stable token you need to change the Publishing status of your Google app from "In testing" to "In production". You can do it here - https://console.cloud.google.com/apis/credentials/consent

taninnazar avatar Nov 16 '23 09:11 taninnazar

Hi @taninnazar thank you quick response, so once changed to "in production" the token will not expire right?

mariamaslam avatar Nov 16 '23 09:11 mariamaslam

Hi @mariamaslam , the token will expire but more rarely - like once a year or once a half year

taninnazar avatar Nov 16 '23 10:11 taninnazar

Amazinggg @taninnazar do i just need to change the permission n that would be it or do i need to re- generate the token with gmail-tester?

mariamaslam avatar Nov 16 '23 10:11 mariamaslam