401 Unauthorized error when trying to get OAuth token via AJAX
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2016 05:04 AM
I'm getting 401 Unauthorized error too when using API in development instance (Helsinki). Under roles, rest_service role description says "Deprecated. This role is no longer required to access the REST API." When creating the user I'm using to connect, I did make sure it has "Web service access only" checkbox checked.
$(document).ready(function () {
//get token
var snOauthClientId = 'my_client_id';
var snOauthSecret = 'my_secret';
$.ajax({
url: 'https://dev_instance.service-now.com/oauth_token.do?grant_type=password&client_id=' + snOauthClientId + '&client_secret=' + snOauthSecret,
type: 'POST',
dataType: 'jsonp',
success: function (data) { console.log(JSON.stringify(data)); },
beforeSend: setHeader,
error: function () { alert('Failed!'); }//this line gets called; browser's developer tools message displays the 401 error
});
});
function setHeader(xhr) {
var snRESTUser = 'username';
var snRESTPwd = 'password';
xhr.setRequestHeader("Authorization", "Basic " + btoa(snRESTUser + ":" + snRESTPwd));
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2016 05:10 AM
I've never tried getting an OAuth token via script. Have you gone to System OAuth> Application Registry and created a credential profile manually?
I found this video to be very helpful when creating my REST/OAuth integration.
ServiceNow Live Coding Happy Hour for 2016-09-30 - OAuth Part 2 - YouTube
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2016 06:35 AM
Yes, Application Registry part is already complete. That's where I got the client id and secret mentioned in my JS code.
Thanks for sharing that video. Will post back after I finish watching it.
EDIT Also want to mention that the script above is in a standalone HTML page sitting outside of ServiceNow. I'm assuming this can be done.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2016 08:42 AM
The examples in the video seem to show requests that originate from within ServiceNow instance and go to outside servers like bit.ly and Google and get their respective data. I'm trying to do the reverse. My application lives outside of SN and I'm trying to access the SN incident table, after getting and passing on OAuth token.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2016 09:01 AM
Thank you for the clarification. At this point I don't believe inbound OAuth is possible. I understand it is coming in an upcoming release.
Unless someone has an alternate method that I'm not aware of... That's the beauty of the community.