# Handling OAuth 2.0 Refresh Tokens

**URL:** <https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015>\
**Category:** APIs\
**Created:** [August 10, 2021, 12:58pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015 "2021-08-10T12:58:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anshjain232](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/anshjain232/32/192700_2.png) [@anshjain232](https://forum.bubble.io/u/anshjain232)\
**Post date:** [August 10, 2021, 12:58pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/1 "2021-08-10T12:58:36Z")

</div>

I have been working with OAuth2 User-Agent flow method of authentication for one of my Google APIs . Here is how I configured it -

 ![OAuth2](https://us1.discourse-cdn.com/bubble/original/3X/0/4/04ef525b5e5404180153e5b3e6674bde69bbf535.png)

Despite checking the access\_type=offline option, I get an error about my token being expired. The APIs stop working after a day or two and the user doesn’t remain logged in forever. How to configure it so that it handles refresh tokens automatically?

---

<div class="post-metadata">

**Author:** ![elmo.gomes](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@elmo.gomes](https://forum.bubble.io/u/elmo.gomes)\
**Post date:** [November 4, 2021, 3:41pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/2 "2021-11-04T15:41:19Z")

</div>

Ecco that. Having the same problem.

---

<div class="post-metadata">

**Author:** ![boston85719](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/boston85719/32/77286_2.png) [@boston85719](https://forum.bubble.io/u/boston85719)\
**Post date:** [November 4, 2021, 3:46pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/3 "2021-11-04T15:46:38Z")

</div>

Is this an issue when you are constructing the API calls or when the user is using the application?

---

<div class="post-metadata">

**Author:** ![lottemint.md](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/lottemint.md/32/26549_2.png) [@lottemint.md](https://forum.bubble.io/u/lottemint.md)\
**Post date:** [November 4, 2021, 3:56pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/4 "2021-11-04T15:56:08Z")

</div>

> [@boston85719](#):
>
> when the user is using the application?

We had the same issue multiple times.

Inexplicably, it randomly stops refreshing tokens. One user may have a valid one without reauthenticating even after 20 days, while another user’s token may expire in a day.

The best way to create a manual OAuth 2.0 flow.

---

<div class="post-metadata">

**Author:** ![segongora9](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/segongora9/32/101371_2.png) [@segongora9](https://forum.bubble.io/u/segongora9)\
**Post date:** [November 4, 2021, 4:02pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/5 "2021-11-04T16:02:28Z")

</div>

Hey @anshjain232 , what Google API are you trying to use? Google Drive?

---

<div class="post-metadata">

**Author:** ![elmo.gomes](https://avatars.discourse-cdn.com/v4/letter/e/a698b9/32.png) [@elmo.gomes](https://forum.bubble.io/u/elmo.gomes)\
**Post date:** [November 4, 2021, 4:56pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/6 "2021-11-04T16:56:04Z")

</div>

In my case is Goggle Calendar

---

<div class="post-metadata">

**Author:** ![robbin](https://avatars.discourse-cdn.com/v4/letter/r/ea5d25/32.png) [@robbin](https://forum.bubble.io/u/robbin)\
**Post date:** [July 12, 2022, 10:24am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/7 "2022-07-12T10:24:33Z")

</div>

Has anyone found a solution to this?

Facing a similar problem with People API where the token expires in a few hrs & the API stops working

---

<div class="post-metadata">

**Author:** ![anshjain232](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/anshjain232/32/192700_2.png) [@anshjain232](https://forum.bubble.io/u/anshjain232)\
**Post date:** [July 12, 2022, 11:14am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/8 "2022-07-12T11:14:40Z")

</div>

Checking this thread after a looong time !

Pathfix seems to be a good solution to this. Auth setup in minutes. Other way is to store the tokens returned from the API in the database.

---

<div class="post-metadata">

**Author:** ![it18](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/it18/32/213409_2.png) [@it18](https://forum.bubble.io/u/it18)\
**Post date:** [July 12, 2022, 12:02pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/9 "2022-07-12T12:02:29Z")

</div>

Or instead of using user-agent flow or paid Pathfix you can also manually handled the OAuth proces by Bubble API connector. But three are more steps needed.  
For example, it could look like this:

1. Redirect user to your google OAuth screen with scopes by URL.
2. when user accept scopes google back them to your redirect URL where you must catch autorization code from URL.
3. then make a API call with your auth code to get Access token and refresh token.
4. now you can make your API call with your Access token in header parametrs. But you must handle refreshing the Access token when it expired (~1h) you will get the exact expiried time in seconds when you get your Access token.

I did this this way, it maybe little complicated but free and reliable.

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 7, 2022, 6:58am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/10 "2022-09-07T06:58:45Z")

</div>

@it18  
This is i’m trying to perform google authentication.

 ![oauth1](https://us1.discourse-cdn.com/bubble/original/3X/6/3/6399d79658d80f581daddd43e815fc752560acfe.png)

here in second screenshot i’m making post call at token endpoint.

 ![oauth2](https://us1.discourse-cdn.com/bubble/original/3X/e/f/ef7740c2b806aa83d802549c4194a7d61ec260c5.png)

but i’m getting below error for that.

 ![oauth3](https://us1.discourse-cdn.com/bubble/original/3X/5/e/5ebb028d282f92e8c647cc55b27f3cf20b46a61e.png)  
I need to resolve this ASAP. Can you guide me with this?  
Thank you

---

<div class="post-metadata">

**Author:** ![it18](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/it18/32/213409_2.png) [@it18](https://forum.bubble.io/u/it18)\
**Post date:** [September 13, 2022, 7:09pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/11 "2022-09-13T19:09:42Z")

</div>

@pratima.dudhewar sorry for late response…  
I think your problem is that you try to use oauth authorization call inside “OAuth2 User-Agent Flow” API call, if you want to make authorization by yourself manualy (with is much better), You must change Authentication to “None - Self handled”.  
Second thing is, did you set your cloud.google account properly?  
Or did you solve your problem yet?

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 14, 2022, 4:23am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/12 "2022-09-14T04:23:37Z")

</div>

@it18  
Thank you for your reply.  
Well i shifted to “None-self handled”.  
This is the first GET call i’m making.

**" [Sign in - Google Accounts](https://accounts.google.com/o/oauth2/v2/auth?client_id=603815555059-qpisv8v9uumole4j.apps.googleusercontent.com&redirect_uri=https://uk-stats.bubbleapps.io/api/1.1/oauth_redirect&scope=https://www.googleapis.com/auth/userinfo.profile&response_type=code)"**

now i made client\_id to be dynamic. And what should be the data type for this call ? Right now it is text. I tried with using Empty and JSON option.

 ![manual_auth1](https://us1.discourse-cdn.com/bubble/original/3X/f/d/fd0f620cebbe3a12fd835facb90e30e61b68b54d.png)

In response body its coming in text format.

 ![manual_auth2](https://us1.discourse-cdn.com/bubble/original/3X/e/9/e974e11e4e01e44c5470afc2e49e39b7dbce29d4.png)  
 ![manual_auth3](https://us1.discourse-cdn.com/bubble/original/3X/8/c/8c2b411a60dc0dafc4a3afd4d094c17cdb132cdd.png)

But when i’m calling this API from front end nothing is initaiting. Also not getting code in URL.  
What is getting wrong? Is this first step correct for google auth?

---

<div class="post-metadata">

**Author:** ![it18](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/it18/32/213409_2.png) [@it18](https://forum.bubble.io/u/it18)\
**Post date:** [September 14, 2022, 9:53am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/13 "2022-09-14T09:53:15Z")

</div>

> [@pratima.dudhewar](#):
>
> This is the first GET call i’m making.
> 
> **" [Sign in - Google Accounts](https://accounts.google.com/o/oauth2/v2/auth?client_id=603815555059-qpisv8v9uumole4j.apps.googleusercontent.com&redirect_uri=https://uk-stats.bubbleapps.io/api/1.1/oauth_redirect&scope=https://www.googleapis.com/auth/userinfo.profile&response_type=code)"**

Don’t make an API call to this url. Just redirect user to this page “Open an external website” Action, and you will recive your code back in url, after granting scopes.  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/f/a/fa8613fb1e1f22a964217ad068daa7b60400c9c5.png)

Or just coppy this link into your broswer.

Then you must make API cal to authorize with recived code:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/5/e/5e760bec156eacc48923cfa9121801f833b93863.png)  
you will recive this data:  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/5/5/55af30f6e3c933a4ec0c70d671306164161fd94d.png)  
Now you have access and refresh tokens and expiriation time, you can mke an API call that you want.  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/2/5/255c160defb7d9ed85b28279d35a0f2040e6f085.png)  
or refresh token if needed:  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/0/0/0028144f66666a6353b762026ae336e7bd99b5be.png)

Hope this could help you

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 14, 2022, 10:07am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/14 "2022-09-14T10:07:50Z")

</div>

@it18  
Ohhh… Amazing… you have explained so well… Thank you so much.  
I just can’t express my feeling when i saw idtoken in your screenshot.  
Will try that right now. I hope it will work for me too.

Btw just one thing can you send me that link “open external website” which you have in screenshot? Also what is that state parameter you put as"12345". Should I copy it from URL?

Also will ping you here if i stuck at any point. Once again thanks a lott. 😀

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 14, 2022, 10:33am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/15 "2022-09-14T10:33:07Z")

</div>

Hey @it18

 ![manualauthentication](https://us1.discourse-cdn.com/bubble/original/3X/1/e/1ef2916d8a365d29f1e6a88b16a59e72426160f3.png)  
 ![manualauthentication1](https://us1.discourse-cdn.com/bubble/original/3X/8/9/89b2ecae47b5d50519e3473dd7b37ec4f6c92981.png)  
 ![manualauthentication2](https://us1.discourse-cdn.com/bubble/original/3X/2/6/26d75813d0806e3850bcd75c2b56a3f0b1f31917.png)  
after selecting the account from consent screen getting below error.  
 ![manualauthentication3](https://us1.discourse-cdn.com/bubble/original/3X/3/2/32e69472e080f81a7332b434d0b0720154b93a6b.png)

Am i missing on something?

---

<div class="post-metadata">

**Author:** ![it18](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/it18/32/213409_2.png) [@it18](https://forum.bubble.io/u/it18)\
**Post date:** [September 14, 2022, 10:48am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/16 "2022-09-14T10:48:32Z")

</div>

> [@pratima.dudhewar](#):
>
> Also what is that state parameter you put as"12345".

This is kind of Temporary ID (not realy important).

Hmmm, at this error i dont know how to help you…  
At this stage, any error you receive should be caused by, an error in the redirect link or an error in Google Cloud console settings.  
Did you set your redirect url and right scopes in google cloud ?  
Share your google console ss, then we maybe find out something wrong.

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 14, 2022, 10:49am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/17 "2022-09-14T10:49:45Z")

</div>

@it18  
Sure will share.

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 14, 2022, 11:04am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/18 "2022-09-14T11:04:27Z")

</div>

Hello @it18  
Please check below screenshots.

 ![googledeveloper](https://us1.discourse-cdn.com/bubble/original/3X/f/8/f8a652f7790172c4761e63eca8b32e834b8cc37f.png)  
Here in second ss the second number uri is redirect uri which i’m using.  
 ![googledeveloper1](https://us1.discourse-cdn.com/bubble/original/3X/e/f/ef0855684b9a4ba328c51765b9748d91f4d99c87.png)

Is that all okay?

---

<div class="post-metadata">

**Author:** ![it18](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/it18/32/213409_2.png) [@it18](https://forum.bubble.io/u/it18)\
**Post date:** [September 14, 2022, 11:44am UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/19 "2022-09-14T11:44:07Z")

</div>

Make sure that This url (that I dont see well)  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/0/2/02672fc67c064d854632bdc7d385c26efc1bcf38.png)  
is in your Authorised redirect URLs  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/5/1/51e2eb00dbe98949d9e54f197f8f38ea62130c18.png)  
I see here you write URLs with “…/api/1.1/oauth\_redirect” but this is redirect url for Automatic OAuth2 - User agent flow, you do it manually so you must type here page url, from your redirect url from your first redirection link, (the page where you try to chatch the code).

In addition remember to include …/version-test/… of the page and url with"?debug\_mode=true" so it gives you 4 possible combinations of URLs.

This is my redirect url  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/0/5/05dc02f1e4669fb4edab572deed0c2a7181ecf39.png)

---

<div class="post-metadata">

**Author:** ![pratima.dudhewar](https://avatars.discourse-cdn.com/v4/letter/p/a8b319/32.png) [@pratima.dudhewar](https://forum.bubble.io/u/pratima.dudhewar)\
**Post date:** [September 14, 2022, 12:42pm UTC](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015/20 "2022-09-14T12:42:30Z")

</div>

@it18  
you mean so say the page from where i open the external website and get the code back.  
In my case the page URL is  
**[https://uk-stats.bubbleapps.io/version-test/manual\_auth?debug\_mode=true](https://uk-stats.bubbleapps.io/version-test/manual_auth?debug_mode=true)**

is that you are saying? Ans should i add this URL in google console?

[Next page](https://forum.bubble.io/t/handling-oauth-2-0-refresh-tokens/164015.md?page=2)
