Outbound HTTP REST (oAuth) query fails
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-08-2017 12:20 PM
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=7f…vv&client_secret=Na…Qc" 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', '7f…vv');
sm.setQueryParameter('client_secret', 'Na…Qc');
var response = sm.execute();
I get the following responses:
- response.getStatusCode() returns: 401
- response.getErrorMessage() returns: Method failed: (/pss/token/) with code: 401 - Invalid username/password combo
- 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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2017 08:24 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2017 01:31 AM
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()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2018 09:49 AM
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() ;