VMware Cloud Community
jinhoe
Enthusiast
Enthusiast
Jump to solution

How to pass REST API Authorization Token to next workflow?

Hi, I need help with passing Authorization Token to next workflow.

I'm using Orchestrator 8.11.2.21452458. Added a REST host and generated 2 workflows from REST operations:

1. Get authToken
2. Add item

I created a new workflow to link #1 and #2 together.

#1 is able to output authToken (string) and after that in #2, I got error Status Code: 401 (Unauthorized: The request requires user authentication). I was able to run #2 in Postman successfully.

This is the default script generated, I only added the Authorization portion. I suspect something is missing/wrong in this script or I've do it all wrong?

//prepare request
//Do not edit 
var inParamtersValues = [];
var request = restOperation.createRequest(inParamtersValues, content);
//set the request content type
request.contentType = "application\/json";
System.log("Request: " + request);
System.log("Request URL: " + request.fullUrl);

//Customize the request here
System.log("authToken: " + authToken);

request.setHeader("Authorization", authToken);

//execute request

Below is the log of the workflow.

Screenshot 2023-06-23 at 4.09.40 PM.png

 

0 Kudos
1 Solution

Accepted Solutions
jinhoe
Enthusiast
Enthusiast
Jump to solution

Guys, I've fixed it. There are 2 issues:

1. When adding a REST Host, the Host's authentication type I used OAuth 2.0, should be NONE instead since I have a dedicated workflow for authentication.

2. The token generated comes with Quotation mark. (Example Token: "DRjODVlNWQtYjVlNC00N"), should be string but without the Quotation mark. (Example Token: DRjODVlNWQtYjVlNC00N). I discovered the abnormality when using System.log to display the token.

Thanks guys for your responses!

View solution in original post

0 Kudos
6 Replies
eoinbyrne
Expert
Expert
Jump to solution

Hi

The simple way to accomplish this would be as follows

1. Modify workflow#1 to store the Authtoken into an Output string

2. Modify workflow#2 to take an Authtoken as an input string

3. Modify your 'wrapper' workflow to have an Variable/Attribute to store the Authtoken and then update the TaskItem for workflow#1 to write the output there. Then modify the TaskItem for workflow#2 to take the stored token as an Input

 

-HTH

jinhoe
Enthusiast
Enthusiast
Jump to solution

@eoinbyrne Yes, that's exactly what I did. Workflow#2 is able to receive authToken from Workflow#1.

But for some reason Workflow#2 can't use it for authentication.

0 Kudos
jinhoe
Enthusiast
Enthusiast
Jump to solution

Maybe the right question should be, how to "apply" Authorization Token in the workflow for REST API?

0 Kudos
bdamian
Expert
Expert
Jump to solution

Usually, Authorization header has a "word" before the Token, for instance: 

request.setHeader("Authorization", "Bearer " + authToken);

 Are you sure that the API is expecting only the token?

 

---
Damián Bacalov
vExpert 2017-2023 (7 years)
https://www.linkedin.com/in/damianbacalov/
https://tecnologiaimasd.blogspot.com/
twitter @bdamian
jinhoe
Enthusiast
Enthusiast
Jump to solution

@bdamian Tried the Bearer too, still same result (status code 401).

I just checked my postman settings and noticed there are 8 hidden settings in the header (Refer below screenshot).

Are all those required request.setHeader for each of them? If yes, how do I set for keys like Postman-Token, Content-Length, Host where the value is <calculated when request is sent>? Please advice, thank you.

Screenshot 2023-06-23 at 8.40.58 PM.png

0 Kudos
jinhoe
Enthusiast
Enthusiast
Jump to solution

Guys, I've fixed it. There are 2 issues:

1. When adding a REST Host, the Host's authentication type I used OAuth 2.0, should be NONE instead since I have a dedicated workflow for authentication.

2. The token generated comes with Quotation mark. (Example Token: "DRjODVlNWQtYjVlNC00N"), should be string but without the Quotation mark. (Example Token: DRjODVlNWQtYjVlNC00N). I discovered the abnormality when using System.log to display the token.

Thanks guys for your responses!

0 Kudos