VMware Cloud Community
Belker
Contributor
Contributor
Jump to solution

Azure REST connection using REST plugin get error grant_type issue

Using vRA\vRO 8.10

I cannot seem to find a resolution to the error below when running an Orchestrator action I've written in JavaScript. The error is returned in the JSON response when attempting to get a token. I have successfully written REST actions against vRA and Infoblox and can't seem to find the magic syntax for Azure. Can anyone provide any insight?

AADSTS900144: The request body must contain the following parameter: 'grant_type'.

I am using the following code:

var request = myresthost.createRequest("POST", "/" + attributeTenantID.value + "/oauth2/token",'{"grant_type":"client_credentials","client_id":"' + attributeApplicationID.value + '","client_secret":"' + attributeClientApplicationSecretKey.value + '"}');

Thanks for reading my post!

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
eoinbyrne
Expert
Expert
Jump to solution

Hi

When I did this had an additional field for the resource URL (also mine was x-www-url-formencoded & not JSON).

 

var resourceURL = "https://management.azure.com"

var bodyContent = "grant_type=client_credentials&" +
	"client_id=" + azClientId + "&" +
	"client_secret=" + clientSecret + "&" +
	"resource=" + resourceURL;
	

var request = loginRestHost.createRequest("POST", "/" + myTenantId + "/oauth2/token", bodyContent);

connectionData.loginRestHost = loginRestHost;

// set the content type
request.contentType = "application\/x-www-form-urlencoded";

System.log("Logging in...");
// Execute the request and get the response
var response = request.execute();
if(response.statusCode != 200)
{
	throw "Azure API Login failed with Status : " + response.statusCode + " & Response : " + response.contentAsString;
}

var tokenContent = JSON.parse(response.contentAsString);
System.log(JSON.stringify(tokenContent, null, 2));
var token = tokenContent.access_token;

 

 

-HTH

 

View solution in original post

2 Replies
eoinbyrne
Expert
Expert
Jump to solution

Hi

When I did this had an additional field for the resource URL (also mine was x-www-url-formencoded & not JSON).

 

var resourceURL = "https://management.azure.com"

var bodyContent = "grant_type=client_credentials&" +
	"client_id=" + azClientId + "&" +
	"client_secret=" + clientSecret + "&" +
	"resource=" + resourceURL;
	

var request = loginRestHost.createRequest("POST", "/" + myTenantId + "/oauth2/token", bodyContent);

connectionData.loginRestHost = loginRestHost;

// set the content type
request.contentType = "application\/x-www-form-urlencoded";

System.log("Logging in...");
// Execute the request and get the response
var response = request.execute();
if(response.statusCode != 200)
{
	throw "Azure API Login failed with Status : " + response.statusCode + " & Response : " + response.contentAsString;
}

var tokenContent = JSON.parse(response.contentAsString);
System.log(JSON.stringify(tokenContent, null, 2));
var token = tokenContent.access_token;

 

 

-HTH

 

Belker
Contributor
Contributor
Jump to solution

Thank you very much!

It was driving me crazy. It didn't like the way i was putting together the body in my request and as you pointed out it wanted the x-www-url-formencoded passed.

Thanks Again!

Happy Holidays!!

Reply
0 Kudos