401 Unauthorized error when trying to get OAuth token via AJAX

greatbear302
Kilo Contributor

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');                
        }
8 REPLIES 8

Chuck do you have an approx. idea on when the next release is coming?


Istanbul is coming in Q4 of this year (really really soon.)


Hi @Chuck Tomasi  and @greatbear302 now after 8 years is possible for how solve it?

 

KavithaSP
Tera Contributor

Are you able to resolve this?