# Integrating API that uses Bearer Token Authorization

**URL:** <https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573>\
**Category:** APIs\
**Created:** [January 9, 2022, 8:47am UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573 "2022-01-09T08:47:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adish](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adish/32/114489_2.png) [@Adish](https://forum.bubble.io/u/Adish)\
**Post date:** [January 9, 2022, 8:47am UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/1 "2022-01-09T08:47:41Z")

</div>

I’m trying to integrate the Zumrails API in my Bubble app. Their [documentation](https://docs.zumrails.com/#authentication) says they use Bearer Token Auth to authenticate requests. The auth endpoint returns a “Token” field that must be used with future API calls. I’m not sure how to proceed with this - which type of auth method to use in Bubble API connector, how to supply the right data so that the calls work, etc.

Here’s a request to the Auth endpoint (using auth type as “None/self-handled”) that works but I don’t know how to use the Token it returns in future calls.

 ![Call that works but doesn't store the token](https://us1.discourse-cdn.com/bubble/original/3X/0/5/05e7e0f4cce91addf16d1945fa6e812aa09d44c8.png)

Here’s another call I tried using auth type as OAuth2 Custom Token that doesn’t work since I don’t know how and where to supply the Token field that’s returned with the response to the authentication endpoint:

 ![Call using OAuth2 Custom Token that doesn't work](https://us1.discourse-cdn.com/bubble/original/3X/5/8/581ecb009b2001507c0ce752b7f0d4a4ff91a4de.png)

Tried searching through Bubble docs, forum posts but still stuck. Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![rowntreerob](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@rowntreerob](https://forum.bubble.io/u/rowntreerob)\
**Post date:** [January 9, 2022, 9:21am UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/2 "2022-01-09T09:21:06Z")

</div>

step 1. using API Connect tool make a call ( initialize ) to the _auth_ endpoint. On success the response , or value of the token can be used until it terminates ( often a year ) .

#2 with the Workflow tool , link together actions making the call above, getting the response AND creating a new Type ( Token.token ) and run this using preview

#3 now you have in data tool what you will need to configure any subsequent workflows making API calls in which an _authorization_ header is required . To config the authorization header use the following

- key = “authorization”
- value =" Bearer tokenValue" ← 1 space between Bearer and the copied token value

---

<div class="post-metadata">

**Author:** ![Adish](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adish/32/114489_2.png) [@Adish](https://forum.bubble.io/u/Adish)\
**Post date:** [January 9, 2022, 9:58am UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/3 "2022-01-09T09:58:28Z")

</div>

I had thought of doing it the way you suggested - storing the token in database, creating a recurring backend workflow to fetch a new token every hour, and using it for future API calls.

But I was wondering if there’s a simpler, more built-in way to handle this scenario. Bearer token auth is a common way of API authentication, and I thought there must be a setting in the API connector to configure this without having to do it via going down the route of storing token in database and refreshing it every hour.

[Bubble API docs](https://manual.bubble.io/account-and-marketplace/building-plugins/adding-api-connections#authentication) suggest they do have a way of handling these cases, I just need help with how I should supply the token field there:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/4/f/4fc9d7f59468077fa0ca344598ab0bf7000842ad.png)

---

<div class="post-metadata">

**Author:** ![jsterling](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jsterling](https://forum.bubble.io/u/jsterling)\
**Post date:** [January 9, 2022, 2:16pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/4 "2022-01-09T14:16:47Z")

</div>

If you’re using one of the OAuth2 flows, Bubble should automatically add the token you receive from the “authorize” endpoint to any future API calls as a Bearer token in the HTTP header. That is to say, you don’t need to add it. Bubble handles it.

If instead you decide to implement a self-handled approach, you’ll need to add a shared header called “authorization” and fetch your bearer token as suggested earlier.

---

<div class="post-metadata">

**Author:** ![Adish](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adish/32/114489_2.png) [@Adish](https://forum.bubble.io/u/Adish)\
**Post date:** [January 9, 2022, 2:47pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/5 "2022-01-09T14:47:17Z")

</div>

This is the response from the authorize endpoint according to Zumrails docs. Will Bubble automatically pick up the “Token” field from this?  
 ![image](https://us1.discourse-cdn.com/bubble/original/3X/8/d/8dfa9b37d856b4d388c122763f17e36211dda825.png)

Here’s the error I’m getting when making a request using the OAuth2 Password Flow:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/1/b/1baae578a76671ad98daf051e542e6273f02c40a.png)

It says “Unsupported media type” so I think the authentication went through but the issue is with the actual API call itself sending a bad payload, or maybe not? I’m not sure.

Here’s the API request that results in the above error:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/1/2/12e23705e014a4d8ef07aae071f9fe7d9f6b2058.png)

---

<div class="post-metadata">

**Author:** ![Jici](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/jici/32/30722_2.png) [@Jici](https://forum.bubble.io/u/Jici)\
**Post date:** [January 9, 2022, 3:03pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/6 "2022-01-09T15:03:27Z")

</div>

You cannot use the User Password oAuth2 token process. This API doesn’t follow oAuth2 guideline. You need to create your own API Call.  
Your first screenshot is ok. You need to manually call the authorizae endpoint and after use the token in header of each call with an Authorization: Bearer yourtoken header

---

<div class="post-metadata">

**Author:** ![rowntreerob](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@rowntreerob](https://forum.bubble.io/u/rowntreerob)\
**Post date:** [January 9, 2022, 3:04pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/7 "2022-01-09T15:04:24Z")

</div>

general on JWT and a good background read…

> **[JWT Security Best Practices](https://curity.io/resources/learn/jwt-best-practices/)**
>
> What you should consider when using JWTs in your applications.

AND as applied in bubble apps [here](https://www.biberei.de/en/how-to-use-data-outside-your-bubble-io-application/)

---

<div class="post-metadata">

**Author:** ![jsterling](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jsterling](https://forum.bubble.io/u/jsterling)\
**Post date:** [January 9, 2022, 3:12pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/8 "2022-01-09T15:12:47Z")

</div>

That response object isn’t going to be picked up by Bubble automatically. You’re probably going to have to implement this manually by designing an API Connector call to fetch the token, identifying the token in the response object, and then passing the token as a parameter in the header of your other API calls. There’s at least one forum entry explaining how to do this.

---

<div class="post-metadata">

**Author:** ![Adish](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adish/32/114489_2.png) [@Adish](https://forum.bubble.io/u/Adish)\
**Post date:** [January 9, 2022, 3:46pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/9 "2022-01-09T15:46:17Z")

</div>

Yeah, so that seems to be the only way I can get this working, like what @rowntreerob suggested. I was trying to avoid going that route since given how many configurations the API connector offers I was thinking there would be a more “built-in” way to do this.

Also, is this the forum entry you’re referring to?

> [@\[Showcase\] Manual OAuth2 Token Integration](http://forum.bubble.io/t/showcase-manual-oauth2-token-integration/79988):
>
> Hey guys, I struggled for a long time to get the OAuth2 working right. Eventhough I felt like I was making progress every day, different errors kept showing up every time, which drove me crazy (and that obviously didn’t help either crazy_face ) Thankfully I found a lot of help on this forum, especially @lottemint.md who very kindly offered his help and walked me through all the steps. I would probably never have succeeded without him! Big up to him and @NigelG for their understanding. I’m in…

---

<div class="post-metadata">

**Author:** ![jsterling](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jsterling](https://forum.bubble.io/u/jsterling)\
**Post date:** [January 9, 2022, 4:04pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/10 "2022-01-09T16:04:58Z")

</div>

That’s one of them (and it is a very good one.) There’s another I saw recently that focuses on the core of what I think you’re going to have to do - create an API Connector call to fetch that token and then create a backend workflow to invoke that connector call on whatever schedule you need to refresh it.

---

<div class="post-metadata">

**Author:** ![rowntreerob](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@rowntreerob](https://forum.bubble.io/u/rowntreerob)\
**Post date:** [January 9, 2022, 4:05pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/11 "2022-01-09T16:05:30Z")

</div>

its more of a brute force force approach than _built-in_ way. Brute force has potential to being the place that you iterate – to improve the built-in nature of your implementation.

The JWT spec when combine with bubble’s widely adaptable toolset almost certainly allows for better implements… in theory like below

1. default bubble app has stored app-level token allowing for one year of calls on the api with a scope == bubble.app.${abc} for read access to endpoint==3rdParty.endpoint  
call this using api connector that was involved in connector.init() type call at any time up to a year ago

2. error handler on above catches err=token expired which will occur after a year  
2.a fallback will RENEW token with same priviledge/scope as above to cover you for year 2  
2.b call the auth endpoint ( not the normal api use endpoint ) of the api , parsing the response.token.value and storing that … either store in a client side cookie OR store in bubble db under the User ( logged in user )  
2.c for the next year ( until token.exp ) just apply the cookies token value ( the renewed token ) back into the default scenario from #1 above

Discussion - normal auth process for mobile apps works along the above guidelines & implement steps where UX for auth involves a ONE\_TIME step of auth that occurs when you install the app… and then user is never bothered by dialog for auth for up to a year when they may once again encounter the dialog over getting renewal on permissions.

that is user level example not example of an entire app being granted permissions on a 3rd party api

**built in approach using plugin - renew the token value before expiration**  
Bubble provides tools / plugin that should ( in theory ) permit scheduled Token renewal on the client ie using a 1 year expiration and renewing the token on 11 month interval with scheduled WF. Token endpoint discussed above would not even be necessary because the bubble plugin permits payload/subject/audience config such that a verifiable token can be generated as needed using the plugin without leaving the client.

The server side endpoint for generating a token is shown below but using the client side shell script at the bottom, a passable token value can be reverse engineered ( and was in bash script ) and that bash gen’d token validated on the server. This demonstrates that with proper docs from the API , in theory, using the bubbles JWT plugins, you can gen your own token using it as required _Bearer_ authorization in standard API auth using tokens.

 ![Screenshot from 2022-01-10 14-50-51](https://us1.discourse-cdn.com/bubble/original/3X/9/6/96a59398d55f619a797c9cdc95ce57695c5c893b.png)

A bubble JWT client could gen token just as well as the bash shell below

 ![bash_jwt](https://us1.discourse-cdn.com/bubble/original/3X/b/9/b9fb415e16f29d2268a3ee73ec3e5baad12729a8.png)

---

<div class="post-metadata">

**Author:** ![Adish](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/adish/32/114489_2.png) [@Adish](https://forum.bubble.io/u/Adish)\
**Post date:** [January 9, 2022, 5:25pm UTC](https://forum.bubble.io/t/integrating-api-that-uses-bearer-token-authorization/185573/12 "2022-01-09T17:25:04Z")

</div>

Thanks a ton. Finally got it working.
