How to request a JWT token and use it in actions

Hello everyone,

I need to receive data from a REST service, but that service require a JWT token as bearer in the header.
I tried the “WEB request” action and it works well, but I need to write the JWT token manually fixed in the properties of the action, is there a way to login in a REST service, receive the JWT token and store it to be used in other actions?

Have you tried to configure authentication via OAuth2 in Weblocation data connector? If your service supports that kind if authentication it should work “out of the box”.

How can I do it? in the Connector configuration I’ve got 3 choose for authentication: none, basic, windows integrated … no oauth2

EDIT: I see that I’ve to update the software, I’ll try!

Hello everyone again, I tried to configure the Oauth2 in the web location connector but I don’t understand how to configure it and the documentation page is empty (https://help.easymorph.com/doku.php?id=connectors:weblocation). Please, can someone tell me how to compile the oauth2 properties?

Check this thread

Thanks for the reply, but I cannot make it work.
I try to explain my situation:

  • I've got a REST API, written by me, that release a valid JWT token.
  • The request is structured like so:
    {
    "userName" : "test" ,
    "password" : "mypassword" ,
    "grantType" : "password",
    "clientId" : "TheClientID"
    }
  • If the request is correct, it releases an accessToken, a refreshToken and an expiration;

So, I started configuring the OAuth2 authentication in the web location as follow:
In the Web Location connector, the Base URL: http://myUrl/api/GetMyData (it's the method that will return me the information if I send it the correct JWT token;

In the "Configure" of the Oauth2 Authentication:

  • Authorization URL: http://myUrl/api/oauth2/token (it's the method that receive the previous request structure and return the JWT);
  • Token URL: same as Authorization;
  • Redirect URI: ./
  • Client ID: myClientID;
  • in the parameters:

userName
password
grantType
clientId

at this point it require an authorization but if I press the button it doesn't work.

If I understand correctly, EasyMorph require a page (Authorization URL) where I can insert my credential and it release a token that will be used in the Base URL, is it correct?

Maybe I'm using a wrong way to operate, so I try to better explain my desire:

  • I've got a POST method that release a valid JWT token, that expire after 30 minutes.
  • I've got several GET methods that require a valid JWT token as authentication bearer in the header;

How I can call the GET methods, passing the JWT token live-generated in the POST method?

Hi Andrea,

if your REST API doesn’t follow the OAuth2 specification, then probably the built-in OAuth2 authentication mechanism in the Web Location connector won’t work.

You can do the following:

  1. Set the base URL in the Web Location connector to http://myUrl/api/
  2. Obtain the authorization token by sending Web Request to path oauth2/token (it will be automatically appended to the base URL specified in the web)
  3. Extract the token from the response, make it a column value. At this point you should have a table with 1 row and 1 column with the token value.
  4. Use the “Iterate” action to call another module, and pass the token from a column to that module’s parameter.

Now, in the called module you have a parameter with the token value assigned. Now you can use it in the authorization header:

image

You can prepend "Bearer " either in the calling module, or in a calculated parameter of the called module. Use the “Preview” mode of the Web Request action to see if the Authorization header is formed correctly.

Hello Dmitry,

I tried your solution, I created a new module with a “AccessTokenParam”, and in the iterate action I setted “AccessTokenParam” = accessToken (the field).

The mail doesn’t show any warning but the module, instead, tells me that “The format of value ‘’ is not valid”. It seems that the value isn’t transferred, but I don’t know why (I tried to manually use the string and the Web Request works fine)

Can you post a screenshot of the error?

Sure, here it is:

The main module with the iteration:
image

And this is the module iterated:

As you can see the parameter is empty, even if I Run all actoins.
I tried to manual copy the accessToken field value into the AccessTokenParam and everything works (but the token expires in 10 minutes).

In the Headers tab of the “Web Request” action, do you have the Authorization header assigned with the token? Can you post a screenshot of that tab too, please.

The parameter is empty because iterations don't affect the module that you see. Instead, under the hood, an iteration creates a invisible clone of the visible module and iterates it. Thus the visible module remains unaffected.

This mechanism allows multiple iterations of the same module at the same time.

Header detail:
image

The parameter is empty because iterations don’t affect the module that you see. Instead, under the hood, an iteration creates a invisible clone of the visible module and iterates it. Thus the visible module remains unaffected.

This mechanism allows multiple iterations of the same module at the same time.
Ah!

I added an Excel Export and, indeed, it exports the file correctly!
So it's only in design time that I see the error in the module, nice, maybe I can insert a default value in the parameter so I can see data even at design time.

Thanks Dmtry!

You're welcome! :slight_smile:

Yes, that's how calls/iterations are intended to work. Module parameters are specified explicitly in the module at design time, and assigned implicitly during calls from other modules.