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.

Outbound HTTP REST (oAuth) query fails

andrewrump
Mega Contributor

Hi

I am currently developing a data retrieval module which first connect to an oAuth REST service to obtain a token which I then use to make a SOAP request - unfortunately ServiceNow does not support using oAuth with SOAP so I have to program it myself.

I have problems getting an access token using https://api.cisco.com/pss/token - it complains that the client is invalid.

I can get it to work using curl:

curl -d "grant_type=client_credentials&client_id=7fvv&client_secret=NaQc" https://api.cisco.com/pss/token

But when I try to do the same thing inside ServiceNow with the following code:

var sm = new sn_ws.RESTMessageV2();

sm.setEndpoint('https://api.cisco.com/pss/token');

sm.setHttpMethod('post');

sm.setQueryParameter('grant_type', 'client_credentials');

sm.setQueryParameter('client_id', '7fvv');

sm.setQueryParameter('client_secret', 'NaQc');

var response = sm.execute();

I get the following responses:

  1. response.getStatusCode() returns: 401
  2. response.getErrorMessage() returns: Method failed: (/pss/token/) with code: 401 - Invalid username/password combo
  3. response.getBody() returns: {"error":"invalid_client"}

I tried to add sm.setRequestHeader('Accept', 'application/json'); before performing the execute() but that also did not make any difference.

I've tried using GlideHTTPRequest() and get the same result.

What am I doing wrong?

Best regards

Andrew Rump

BusinessNow

7 REPLIES 7

One other potential issue - are you working in a global or scoped app? It looks like gs.urlEncode() is a scoped-only API. If you're working in a global app, use GlideStringUtil.urlEncode() instead of gs.urlEncode(). If there are no special characters in the Client ID / Secret, you can just add the values to the body string directly without encoding them.


Thanks for looking into it joshua.nerius but it still doesn't work! 😞


There are no special characters (only letters and numbers) so it also fails without the urlEncode()


udaykemburu
ServiceNow Employee
ServiceNow Employee

 

Have a created a OAuth profile and used the 

GlideOAuthClient 

 methods?

as per the below documentation.

https://docs.servicenow.com/bundle/kingston-application-development/page/app-store/dev_portal/API_reference/GlideOAuthClient/concept/c_GlideOAuthClient.html

 

Here is some sample code that worked for me.

var oAuthClient = new sn_auth.GlideOAuthClient();
var requestor_context = 'test';
var requestor_id = 'abc@xyz.com';
var oauth_profile_id = '<sys_id_of_oauth_profile>';

var params = {grant_type:"password", username:'admin', password:'password', oauth_requestor_context:requestor_context, oauth_requestor:requestor_id, oauth_provider_profile:oauth_profile_id};
var json = new global.JSON();
var text = json.encode(params);
var tokenResponse = oAuthClient.requestToken('<oauth_profile_name>', text);
var token = tokenResponse.getToken();
var access_token = token.getAccessToken() ;