Automatic Refresh of Google Access Tokens

Hi! i currently have a system that automatically refreshes the users access token 10 minutes before it expires. BUT the user has to be active on the screen for it to happen!

I am looking for a way that i can handle this automatically in the backend, with no need of using the front end.

Can anybody help?

I have in the db the refresh token which i then use to torigger an api call to google for it to refresh the token.

Why do you need that? Do you need to access Google data when user is not active?

If yes, schedule a backend wf expiration date -10min that refresh token and schedume itself fro the refresh token.

Also, any reason why you are not using user-agent auth in API connector? This way you don’t need to care about refresh token

As Jici says, if you need to perform some backend opérations that does not require the user to be there you can use your API tokens. Do you have any more context to provide? Thanks!

Referring

I think you are not using API workflows to create the new token or to refresh the token.

You should create two API workflows.

  1. GetToken
  2. RefreshToken

GetToken should generate the token for the first time when the user connect the google account for the 1st time. Within the GetToken workflow, you should schedule RefreshToken to run after X mins (depends on the token expiry)

RefreshToken will use the refresh token you got from the 1st call to refresh the token and get a new access token. Once you get the new access token, update the db entry with new data (tokens and expiry). Within this workflow add another schedule workflow action to schedule itself (RefreshToken) after X mins. This will take care of subsequent run for the future.

I hope this helps.

Feel free to reach out if you need more help.

1 Like

how can i refresh token with

@frejusombrax765

I would recommend you to go through this documentation by Google to get clear understanding of how OAuth works.

Hi! i currently do the whole process manually, i start the 0auth flow with the user login in with google and retrieving an access code in the url parameter that then triggers to fecth an access token!

what i need it for that users access to be refreshed before it expires cause my app triggers api calls for google ads and the user won’t be active probably

Send the request to a backend WF that reschedule itself before token expire. Each time. For each user.

@orka

Set up two API endpoints like shown in the screenshot attached.

Let me know if this helps.

@codefree out of interest do you find these ever failing? if so what do you do to mitigate this. the reason i ask is that i take this approach but its not consistent periodically the token is not refreshed.

1 Like

@Bubbleboy I have been using this for years now, So far I haven’t faced any issue with this approach if the configuration is done correctly. This is strictly set up as per google’s official documentation for OAuth.

If you have any specific use case, I would be happy to deep dive with you and try to identify the root cause for the same.

1 Like

@codefree thanks for the quick reply and confirmation its reliable. this must by on my end which i can investigate and solve.

1 Like

I have this exact same set up! the thing is, how do i make it so it automatically refreshes the otken before it expires after 1 hour? wihtout the user being active in the site? cause my users create rules that trigger at another moment, probably after their tokens had epxired, but without being present in the site, it won’t refresh.

i had like if current time > -5 minutes token expiry, trigger that api call to refresh it. but that need the user to actually be in the site

thanks in advance

@orka

Save the access token, refresh token and expiry along with user, in the user table or a new datatype.

You can maintain one row for each user and keep updating it using backend workflow.

OR

You can create new row each time yoh get a new token.

You can keep the new datatype as private and ignore privacy rules in the api workflow to schedule actions for relevant user.

If you need a 1-on-1 with me, I can get on a call tomorrow. You can book it here. https://meeting.codefree.io (Free 30 mins)

JUST SOLVED IT! I was able to refresh it on the backend. thank you so much

THANKS!

Can you record one-to-one to share it with us ?