Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

How to get a token from invokeHTTP to use it in another invokeHTTP without losing the flow content

avatar
Contributor

Hello

I have the following :

  • Tomcat server (local)
  • Nifi (local)
  • customer csv file

In my flow I am getting the file content and transforming it to a json body (the body I need to send it in the API post).

The problem is to use the invokeHTTP processor to send the api (onboard) I need to get a token via another API (auth) and add it in the header of the main api (onboard).

FediMannoubi_0-1682503278277.png

 

Auth API : 

Sends a json to the tomcat :

 

 

{
    "user_name": "User",
    "password": "Password"
}

 

 

And get a json with a token valid for 2 min

 

 

{
    "id": 1,
    "user_name": "user",
    "token": "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdXBlcmFkbWluIiwidGV...",
    "roles": [
        "USER",
        "ADMIN"
    ],
    "homePage": null,
    "priorite": 0
}

 

 

 

The problem is when I pass the customers json to the auth API it will take that json as the body for that API and I lose my customer json.

Is there another way to get the token from the auth API and use it in the onboard API header without losing the customers json (main flow)?

 

1 ACCEPTED SOLUTION

avatar
Contributor

I used a global variable (DistributedMapCacheClientService & DistributedMapCacheServer) and a separate process group to run each 2 min to get a token then store that token in my global variable and use it in the header of my OnBoard API and each time i get a 403 error I loopback my flow file to request new token to use

UpdateAttribute before the InvokeHTTP is used to get the exact token text from the full response from the oauth API 

FediMannoubi_0-1684766530532.png

InvokeHTTP
 

FediMannoubi_1-1684766563296.png

Get Auth Token (Cycle)
 

FediMannoubi_2-1684766581349.png

 

View solution in original post

3 REPLIES 3

avatar

@FediMannoubi Get the required response data that you need from flowfile to an attribute with EvaluateJsonPath.  Do this for user, password, and token.     Once these  data values are attributes,  you can use them in the header, or write them back out to the flowfile content when appropriate.

avatar
Contributor

I used a global variable (DistributedMapCacheClientService & DistributedMapCacheServer) and a separate process group to run each 2 min to get a token then store that token in my global variable and use it in the header of my OnBoard API and each time i get a 403 error I loopback my flow file to request new token to use

UpdateAttribute before the InvokeHTTP is used to get the exact token text from the full response from the oauth API 

FediMannoubi_0-1684766530532.png

InvokeHTTP
 

FediMannoubi_1-1684766563296.png

Get Auth Token (Cycle)
 

FediMannoubi_2-1684766581349.png

 

avatar
Expert Contributor

It sounds like you are using OAuth, which means you ought to be using an OAuth2AccessTokenProvider controller service.  See my screen shot, which shows that inside the InvokeHTTP there is a configuration line that lets you specify this controller service.  This service automatically handles getting the token for you and embedding it in the header so your flowfile remains intact.  This will greatly simplify your current flow because you can get rid of the map cache and that whole process cycle.

 

ChuckE_0-1684767128083.png