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.

Rest API OAuth returns not Authorized error

dhanuka1
Giga Contributor

I managed to access the Rest Api with Basic authentication with username and password.

Now I am trying to access the Rest Api with OAuth.

I was referring to http://http//wiki.servicenow.com/index.php?title=Generating_OAuth_Tokens#API_Request_Parameters&gsc....

This is the format of the POST message,

Host: devxxxxx.service-now.com

Accept: application/json

Content-Type: application/json

Authorization: Basic YWRtaW46Q0BycjBsMTIz

Cache-Control: no-cache

Content-Type: application/x-www-form-urlencoded

Url Encoded body:

grant_type=password&client_id=xxxxxx&client_secret=xxxxxx&username=admin&username=xxxxxx

I tried both encoded body and body without encoding, it gives the same result.

Response

{

      "error": "server_error",

      "error_description": "access_denied"

}

Am I missing something here? Doesn't the "admin" user has privileges to perform this operation and acquire tokens?

1 ACCEPTED SOLUTION

Ok, two things,



1. Mistake,



grant_type=password&client_id=xxxxxx&client_secret=xxxxxx&username=admin&username=xxxxxx



SHOULD BE,



grant_type=password&client_id=xxxxxx&client_secret=xxxxxx&username=admin&password=xxxxxx



2. For this request you should not have the Authorization Header. I removed the Basic Authorization header. it's working now.



Recap,



Using POSTMAN,



Make sure the endpoint is "https://instance-now.com/oauth_token.do "



Headers:


Accept: application/json



Body (x-www-form-urlencoded)



grant_type : password


client_id: xxxxxx


client_secret: xxxxxxx


username: username_01


password: password_01



Hope this will help someone....


View solution in original post

5 REPLIES 5

dhanuka1
Giga Contributor

Also I forgot to mention that it's posting to /oauth_token.do endpoint.



POST /oauth_token.do HTTP/1.1


Ok, two things,



1. Mistake,



grant_type=password&client_id=xxxxxx&client_secret=xxxxxx&username=admin&username=xxxxxx



SHOULD BE,



grant_type=password&client_id=xxxxxx&client_secret=xxxxxx&username=admin&password=xxxxxx



2. For this request you should not have the Authorization Header. I removed the Basic Authorization header. it's working now.



Recap,



Using POSTMAN,



Make sure the endpoint is "https://instance-now.com/oauth_token.do "



Headers:


Accept: application/json



Body (x-www-form-urlencoded)



grant_type : password


client_id: xxxxxx


client_secret: xxxxxxx


username: username_01


password: password_01



Hope this will help someone....


jsubat
Kilo Contributor

Thanks, Dhanuka. That was enormously helpful.


This is not working for AD/LDAP integrated credential, Please advice