-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Labels
priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Description
Environment details
- Node.js version:
v12.22.8 - npm version:
6.14.15 googleapisversion:72.0.0googl-auth-libraryversion:7.0.4- environment variabels:
clientId,clientSecret,redirectUri
Issues
- google cloud functions are implemented and are using
gmailservice fromgoogleapisandoAuth2Clientfromgoogle-auth-librarypackages - tokens are created when login into a gmail service account and stored in a db from which are read when tokens are needed
- tokens are refreshed every hour by setting a cron job on a cloud function that refreshes tokens
- after a week tokens get invalid even after refreshing them every hour
Cloud functions used in application
cloud functions are deployed on gcp
functions 1) and 2) are triggered only once
on function 3) is set the cron job every hour
on function 4) is set the cron job every day
Functions used:
1) Login
- instantiate
oAuth2Client
new googleAuth.OAuth2Client({ clientId, clientSecret, redirectUri })
- redirect to url to login into gmail service account
oAuth2Client.generateAuthUrl({
access_type: 'offline‘,
scope: ['profile‘, 'email‘, ‚https://www.googleapis.com/auth/gmail.modify']
})
- when the gmail is authorised, it’s redirected to the cloud function
Handle oAuth2 Callbackaddress with the query parametercodewith which new tokens are received
2) Handle oAuth2 Callback
- instantiate
oAuth2Client
new googleAuth.OAuth2Client({ clientId, clientSecret, redirectUri})
- call a method
getTokenon an instance ofoAuth2Clientand pass a parameter as a query parameter from request obtained from a login function - the method returns a new token object
({access_token, expiry_date, id_token, refresh_token, token_type, scope})which is stored in a database - call method
setCredentialson an instanceoAuth2Clientand pass created tokens as an argument - call
google.options({ auth: oAuth2Client })so the gmail service can be used with the service account - call
watchmethod
gooogle.gmail( 'v1‘).users.watch({
userId: email,
requestBody: {
labelIds: ['INBOX`],
topicName: PUBSUB_TOPIC,
}
})
variable email is passed from a method google.oauth2('v2‘)userinfo.get() and PUBSUB_TOPIC is set in a google console for the service account
3) Sync mails
- instantiate
oAuth2Client
new googleAuth.OAuth2Client({ clientId, clientSecret, redirectUri})
- read stored tokens object from db
- call method
setCredentialson an instanceOAuth2Clientand pass tokens as an argument - call method
getAccesTokenonoAuth2Clientinstance to refresh tokens validity - call
google.options({auth: oAuth2Client}) - process messages from gmail service account
4) Refresh Watch
- instantiate
oAuth2Client
new googleAuth.OAuth2Client({ clientId, clientSecret, redirectUri})
- read stored tokens object from db
- call method
setCredentialson an instanceOAuth2Clientand pass tokens as an argument - call method
getAccesTokenonoAuth2Clientinstance - call
google.options({auth: oAuth2Client}) - call
watchmethod
gooogle.gmail('v1‘).users.watch({
userId: email,
requestBody: {
abelIds: ['INBOX`],
topicName: PUBSUB_TOPIC,
}
})
email is passed as a me string and PUBSUB_TOPIC is set in a google console for the service account
Steps done to try to fix the issue with invalid tokens
- every time after creating a new intsance of
oAuth2Client, callonmethod and listen on an eventtokensto update tokens in db, but this didn't help
on.('tokens', (tokens) => {
...
})
- when tokens are created check the validity of tokens on website (https://www.googleapis.com/oauth2/v1/tokeninfo?access_token), (https://www.googleapis.com/oauth2/v3/tokeninfo?id_token), to check the validity -> tokens were valid
- obviously after a week checking the validity of tookens were error
invalid_grantoccured -> tokens were invalid
Error that is showing up:
error: {
code: "400"
message: "invalid_grant"
stack: "Error: invalid_grant
at Gaxios._request (/workspace/node_modules/gaxios/build/src/gaxios.js:129:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async OAuth2Client.refreshTokenNoCache (/workspace/node_modules/google-auth-library/build/src/auth/oauth2client.js:174:21)
at async OAuth2Client.refreshAccessTokenAsync (/workspace/node_modules/google-auth-library/build/src/auth/oauth2client.js:198:19)
at async OAuth2Client.getAccessTokenAsync (/workspace/node_modules/google-auth-library/build/src/auth/oauth2client.js:227:23)"
}
Metadata
Metadata
Assignees
Labels
priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.