Skip to content

Instantly share code, notes, and snippets.

@imtrinity94
Created March 27, 2023 08:03
Show Gist options
  • Save imtrinity94/5c0eabe54bd292cac1d35acda96cab01 to your computer and use it in GitHub Desktop.
Save imtrinity94/5c0eabe54bd292cac1d35acda96cab01 to your computer and use it in GitHub Desktop.
Generate vRA Bearer Token using vRO Action
if(arguments.callee.name.substr(6)) System.setLogMarker("ACTION: "+arguments.callee.name.substr(6));
var vraRestHost = vraHost.createRestClient();
System.log("vRA hostname: "+vraHost.name);
var loginObj = {};
loginObj.domain = domain;
loginObj.password = password;
loginObj.username = username;
var loginBody = JSON.stringify(loginObj);
var operationUrl = "/csp/gateway/am/api/login";
var request = vraRestHost.createRequest("POST", operationUrl, loginBody);
request.setHeader("Content-Type", "application/json");
var response = vraRestHost.execute(request);
if (response.statusCode != 200) {
System.warn("Request failed with status code " + response.statusCode);
} else {
System.log("Status code: "+response.statusCode);
var res = JSON.parse(response.contentAsString);
var token = res.cspAuthToken;
System.log("Token generated successfully");
return token;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment