The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Getting Error when trying to get access token using REST API

rakesh_mishra
Giga Contributor

Hi,

I am getting below error when my customer is trying to generate access token.

Error : no json object could be decoded.

this error he has sent me using screen shot, and i can't access the log lines also?

when i am trying using my servicenow account it's giving the access token.

Can any one please help me for the same.

here is my python code.

data = {}

data['client_secret'] = client_secret

data['client_id'] = client_id

data['grant_type'] = grant_type

data['username'] = username

data['password'] = password

try:

                      response = requests.request(method='POST', url=utility.TOKEN_URL.format(instance_name), data=data)

except requests.HTTPError as re:

                      _errormsg = {}

                      _errormsg['error_code'] = re.response.status_code

                      if re.response.status_code == 401:

                              raise Exception('Error: Either the credentials provided are invalid or {} does not have the required privileges. Please check your credentials and configuration and try                                                                       again.'.format(username))

                      _errormsg['error_description'] = re.response.text

                      raise Exception(_errormsg)

response = json.loads(response.text)

return response

I think its not able to decode using   json.loads, but i don't know what error it's giving , can any one help me for the same.

1 ACCEPTED SOLUTION

Geoffrey2
ServiceNow Employee
ServiceNow Employee

Actually your 403 error looks like an IP restriction. Check with the ServiceNow admin to see if they need to allow the IP 43.242.244.50


View solution in original post

6 REPLIES 6

Geoffrey2
ServiceNow Employee
ServiceNow Employee

Yikes.   I only speak JavaScript.   Since you're writing in Python I assume you're trying to create a web service that is sending a message to a ServiceNow instance to get an access token, and the ServiceNow instance is sending back that error?


Have you added the Content-Type and Accept headers as application/json?


Hi Geoffrey,


Thanks for prompt reply.



I didn't use content-Type as Application/json but for all other instance which i tried it's sending the JSON response either it's succeed or failed .



But in case of customer Account we are getting above error. i think might be customer has defined some ACL and i am getting above error.



i am new to servicenow can you please help me for the same?


rakesh_mishra
Giga Contributor

Hi Geoffery,



I tried customer instance and just try to give dummy data[client_id, client_secret, username,password] and it fails with above error and in response it gives.



URL: url="https://customer_instance.service-now.com/oauth_token.do"


Output


status code: 403


Error:           <html><head><title>ServiceNow - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 403 - Access restricted (43.242.244.50 not authorized)</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Access restricted (43.242.244.50 not authorized)</u></p><p><b>description</b> <u>Access to the specified resource has been forbidden.</u></p><HR size="1" noshade="noshade"><h3>ServiceNow</h3></body></html>




when i tried with my instance url with wrong client_id and client_secret, then it gives below error.


URL: ="https://my_instance.service-now.com/oauth_token.do"


401 {"error":"server_error","error_description":"access_denied"}



I am not able to understand why it's giving different response



could you please help me to figure out the problem?



Thanks


Rakesh


Geoffrey2
ServiceNow Employee
ServiceNow Employee

I've never used the OAuth authentication method before, so I've been looking into it.


I kept receiving the 401 error until I got the format of the request right.



POST /oauth_token.do HTTP/1.1


Host: instance_name.service-now.com


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



grant_type=password&client_id=c89625b14939621234bbf571d5b2d8e4&client_secret=myClientSecret&username=admin&password=myPassword



The body must be URL encoded, not JSON.   Only the response is in JSON format. And you need the header Content-Type: application/x-www-form-urlencoded



This video is useful: How to Authenticate to ServiceNow REST APIs Using OAuth - YouTube



I never received the 403 error, so I'm not sure yet what would be causing that.