Support Questions

Find answers, ask questions, and share your expertise

Getting Access Token

avatar
New Contributor

Hi. I'm new to working with Apache Nifi. Currently, I've Apache Nifi 2.3.0

I'm trying to get access_token so that I can use it in future API calls. Currently, I use Postman to test getting access_token value and it works. I want to do this via Nifi.

Here is what I setup Postman to get access_token

I'm currently using Postman in order to call an API that generates access_token that I will use later to call other APIs. I want to do the same using Apachi Nifi 2.3.0. The Apache Nifi should generate a file called tokens.txt that contains the access token. Here is what I do in Postman:

POST URL: https://URL/auth/token

Headers:

Content-Type: application/x-www-form-urlencoded;charset=UTF-8

Authorization: Basic <BASE_64_ENCODED_CLIENT_CREDENTIALS>

Body:

grant_type: client_credentials

The response is in JSON format and contains the access token, expiration time of the access token.
Here is an example response:
{
"access_token": "eyJ4NXQjUzI1NiI...KtK5elB38rcAbgFtVP9A",
"token_type": "Bearer",
"expires_in": 3600
}

 

What do I need to achieve?

I want to simulate the above in Apache Nifi where I get the access_token and then store it in a file. Every time a new access token arrives, it needs to re-write the value.

I tried to use the following components

GenerateFlowFile -> EncodeContent -> UpdateAttribute (headers and body) -> InvokeHTTP -> LogAttribute (API-Response) -> EvaluateJsonPath -> UpdateAttribute (filename) -> PutFile

 

But for some reason, the InvokeHTTP was not working (not getting the response in JSON format).

2 REPLIES 2

avatar
Community Manager

@BuffaloDaddy Welcome to the Cloudera Community!

To help you get the best possible solution, I have tagged our NiFi experts @MattWho @SAMSAL @Shelton  who may be able to assist you further.

Please keep us updated on your post, and we hope you find a satisfactory solution to your query.


Regards,

Diana Torres,
Community Moderator


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

avatar
New Contributor

Thanks @DianaTorres . So, let me give more details about my issue. Below is how my process group look like:

1) GenerateFlowFile Processor: I define there the attributes that I want to pass in InvokeHTTP processor. This includes:

  • Authorization
  • content-type
  • grant_type

2) LogAttribute-BeforeINVOKE: This processor is to log attributes in Logfile before HTTPInvoke

3) InvokeHTTP: This processor is where I make API call to get access_token. (Screenshots below). I've the property Request Body Enabled set as true.

4) LogAttribute-AFTERINVOKE: This processor is to log attributes in Logfile after HTTPInvoke

My observations: 

  • If I make the request using curl "e.g. curl -X POST https://SERVER_NAME/auth/token -H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" -H "authorization: Basic CLIENT_SECRET_VALUE" -d "grant_type=client_credentials", I get access_token.
  • I'm suspecting that InvokeHTTP processor when sending the request, some of the parameters might be not parsed properly because of special characters in content-type or authorization.

I hope that  @MattWho or @SAMSAL or @Shelton can help me with this. It's driving me crazy.

BuffaloDaddy_0-1741929804684.png

BuffaloDaddy_3-1741930421274.png

 

BuffaloDaddy_1-1741930049540.png

BuffaloDaddy_2-1741930148703.png

Attributes of InvokeHTTP (SEND)

BuffaloDaddy_4-1741930787650.png

Attributes of InvokeHTTP (ATTRIBUTES_MODIFIED)

BuffaloDaddy_5-1741930975480.png