How to Append dynamic Authorization Token OnBeforeRequest?
Application Type
Mobile
Service Studio Version
11.11.4 (Build 43813)
Platform Version
11.12.1 (Build 30548)

Hey.

I'm trying to connect an app to a REST API, and cannot seem to understand how to add dynamic bearer tokens.

My question is: How do I assign the localvariable BearerToken with the KeyStoreValue in the OnBeforeRequest function inside the REST API call?


This is what I've done:

  1. Get JWT Token from Rest API Service.
  2. I store the Auth key using the KeyStorePlugin, using client actions (SetValue, GetValue).
    1. Store: [API_STORE_NAME]
    2. Key: "access_value"
    3. Value: [BEARER_TOKEN]
  3. I created an OnBeforeRequest and thought there could be some form of Input variable, but there is only a local variable.
    1. I created "BearerToken" local variable.
  4. ?


Issues:

  • From the OnBeforeRequest callback function, I cannot set input variables from the client side.
  • From the client side, I cannot edit the OnBeforeRequest function.
  • I cannot create a client- or server action to integrate the two.


Free cookies for the one that can help ??

Hi @Sherif Diab. Thanks for the reply & screenshots. It actually helped to see your solution :)

However, I solved it by doing as @Kilian Hekhuis suggested, to put an authorization header as an input parameter.

I've uploaded an image of how it looks, in case anyone else ends up having the same crazy ideas as me.

mvp_badge
MVP

Why do you need to add it on the OnBeforeRequest? Can't you add it as an Input Parameter to every Method?

Since it would require me to do it  >500 times, since the API has that many methods. I can't seem to mark all methods either, and add an input parameter there.

Is that really the only option? Seems time consuming. I thought the whole point of OnBeforeRequest was to append headers etc, just like this use case?

mvp_badge
MVP

I understand, that would indeed be quite some work (though an API with 500 methods, that you all need to use, is quite crazy).

The problem is that you have to have a token per user, and it needs to be sent somehow to the server, if you don't want to store it on the server.

Hello @Taha Salim Taha,

As i understood from your example, you don't need to add the "bearer" token in the onBeforeRequest action, you should add it in the headers of your request like the following screenshot


Then you can call this API from the frontend client action like this


This should work with you.


Note: from the architecture standards calling API from the frontend layer is NOT recommended, you have to separate the API(service) layer (ex: ModuleName_IS for Integrations Services), please refer to this discussion  

mvp_badge
MVP

Hi Sherif,

This is discussed already above, you are giving a duplicate answer, that wasn't a good solution for the OP, as explained by them three days ago! Please first read the replies of others, before suggesting something, thanks.

Hi @Sherif Diab. Thanks for the reply & screenshots. It actually helped to see your solution :)

However, I solved it by doing as @Kilian Hekhuis suggested, to put an authorization header as an input parameter.

I've uploaded an image of how it looks, in case anyone else ends up having the same crazy ideas as me.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.