Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Azure integration via REST API

Moira
Giga Contributor

Hi all,

I'm trying to set up a scheduled REST call between ServiceNow and Azure but I can't work out how to get the authentication token so I can use that to complete the request as expected. This is the code I'm using at the moment:

var tenantID = '{tenantID}';
var clientID = '{clientID}';
var subscriptionID = '{subscriptionID}';
var clientSec = '{clientSecret}';
 
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://login.microsoftonline.com/'+tenantID+'/oauth2/v2.0/authorize?client_id='+clientID+'&response_type=id_token&scope=openid&response_mode=fragment&state=12345&nonce=678910');
request.setHttpMethod('POST');

var response = request.execute();

And it's just coming back with the response body as "undefined" and it blats in a HTML page for logging in, so I assume that's wrong.

I've also tried the following:

var tenantID = '{tenantID}';
var clientID = '{clientID}';
var subscriptionID = '{subscriptionID}';
var clientSec = '{clientSecret}';
 
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://login.microsoftonline.com/'+tenantID+'/oauth2/token');
request.setHttpMethod('POST');
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

var reqBody = "{'grant_type':'client_credentials', 'client_id':"+ clientID +",'client_secret':"+ clientSec+",'resource':'https://management.azure.com'}";
request.body = reqBody;

var response = request.execute();

But that failed with the following error (some details have been removed):

*** Script: {"error":"invalid_request","
error_description":"AADSTS900144:The request body must contain the 
following parameter: 'grant_type'.\r\nTrace ID: {}\r\nCorrelation ID: {}\r\n
Timestamp: 2020-07-24 07:52:48Z","error_codes":[900144],"
timestamp":"2020-07-24 07:52:48Z","trace_id":"{}
","correlation_id":"{}","error_uri":"
https://login.microsoftonline.com/error?code=900144"}

I've spoken to some of our internal peeps and we can't work out how to get it working. Can anyone provide any assistance?

1 ACCEPTED SOLUTION
3 REPLIES 3

The first link helped immensely. Thanks Sudhanshu! 🙂 I've got it working now.

Pleasure Moira!!