VMware Cloud Community
jabersuper
Contributor
Contributor
Jump to solution

Help with Authentication. using Username and Password instead of Token

Hi everyone;

forgive my ignorance but I am new to the vRO World and a bit lost at the moment. I want to make some restful requests using username and password instead of tokens! Iat the moment the code i have is using a configuration element named authorization which stores the token. my Question is, what should I change to achieve my  quest.. thanks very much in advance!

System.log("****STARTING WORKFLOW****");

//Getting the header parameters for Authentication

headerParam_0 = HomeAutomationAttributes.getAttributeWithKey("Authorization").name;

headerParam_1 = HomeAutomationAttributes.getAttributeWithKey("Authorization").value;

//The parameters of the action to be executed (e.g.: the entity_id of the light/switch you want to turn off/on).

var request = restOperation.createRequest(null,content);

//Set the headers based on the attributes in the vRO configuration element

request.setHeader(headerParam_0,headerParam_1);

//set the request content type to application/json (set as attribute in Workflow)

if (System.getModule("com.vmware.library.http-rest.configuration").hasHttpMethodHasBodyPayload(request.getMethod())) {

   System.log("Setting defaut content type to:  " + defaultContentType );

   request.contentType = defaultContentType;

}

//Log the Request Details

System.log("Request: " + request);

System.log("Request URL: " + request.fullUrl);

//Execute the request

var response = request.execute();

//Log the response, useful for troubleshooting

System.log("Response: " + response);

statusCode = response.statusCode;

statusCodeAttribute = statusCode;

System.log("Status code: " + statusCode);

contentLength = response.contentLength;

headers = response.getAllHeaders();

contentAsString = response.contentAsString;

System.log("Content as string: " + contentAsString);

System.log("****COMPLETED****");

Smiley Happy

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

This code executes the request by calling request.execute() method. You can replace it with another method call - request.executeWithCredentials(username, password)

However, replacing the execution call may not be enough. There are some other things to check:

  • Does the REST endpoint you call support authentication with username/password? Maybe it supports only some form of token authentication and need some configuration in order to enable basic authentication?
  • You may need to remove the code that sets the request parameters
  • You may need to change the REST host configuration in vRO (using the workflow to update REST host or the workflow to remove/add it a new), ensuring that the host is added with Basic authentication.

So, in general, changing the scripting code alone might, or might not work, depending on the REST endpoint specifics.

View solution in original post

Reply
0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

This code executes the request by calling request.execute() method. You can replace it with another method call - request.executeWithCredentials(username, password)

However, replacing the execution call may not be enough. There are some other things to check:

  • Does the REST endpoint you call support authentication with username/password? Maybe it supports only some form of token authentication and need some configuration in order to enable basic authentication?
  • You may need to remove the code that sets the request parameters
  • You may need to change the REST host configuration in vRO (using the workflow to update REST host or the workflow to remove/add it a new), ensuring that the host is added with Basic authentication.

So, in general, changing the scripting code alone might, or might not work, depending on the REST endpoint specifics.

Reply
0 Kudos
jabersuper
Contributor
Contributor
Jump to solution

Thanks very very much! It works like a charm

Reply
0 Kudos