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

Getting 401 Error when trying to access the records using Access-Token

rakesh_mishra
Giga Contributor

Hi,

I am trying to access records using rest api, but some times, not every time , i get 401 error, could you please help me to understand why i am getting 401 error.

here are the streps , what i am trying to do

        1.generate Access token and refresh token and store in DB.

        2. trying to access data from table using rest api [record_url="https://{}.service-now.com/api/now/table/{}?sysparm_query=sys_updated_on>={}^ORDERBY sys_updated_on]

        3. if it fails or give 401 error then trying to refresh access token.

        4 if still getting 401 error then exit

but most of the time above code works perfectly but some times it gives 401 error.

Could you please help me to understand why i am getting 401 error.

Note: if i don't have access then it should not generate Access token and refresh token

4 REPLIES 4

bernyalvarado
Mega Sage

Hi Rakesh,



It is possible that sometimes you're hitting the 401 (unauthorized error) because you're accessing specific records that you don't have access to. This security will be enforced either by ACLs or by onBefore query business rules.



Thanks,


Berny


Thanks Berny, for prompt reply.


but i am not asking for specific records, i am always send below request.


"https://my_instance.service-now.com/api/now/table/incident?sysparm_query=sys_updated_on>={}^ORDERBY sys_updated_on.



but some time it doesn't work


Hi Rakesh,



But the records you're requesting (the result) will always vary depending on what parameter you're passing to the query string (highlighted below as thisparameter)



t?sysparm_query=sys_updated_on>={thisparameter}^ORDERBY sys_updated_on



Thanks,


Berny


Hi Berny,



yes, 'sys_updated_on>={thisparameter}' thisparameter will always update, i will   try to explain with an example.



Suppose we have some records


id                     sys_updated_on (time)


id1                   10


id2                   11


id3                   12



if i will send below request.


https://my_instance.service-now.com/api/now/table/incident?sysparm_query=sys_updated_on>=10^ORDERBY sys_updated_on.


result should be


[id1,id2,id3]



if query



if i will send below request.


https://my_instance.service-now.com/api/now/table/incident?sysparm_query=sys_updated_on>=12^ORDERBY sys_updated_on.


result should be


[id13]



if i will send below request.


https://my_instance.service-now.com/api/now/table/incident?sysparm_query=sys_updated_on>=13^ORDERBY sys_updated_on.


result should be


[]



but i am getting 401 error it is unauthorized error.


if i request for a record which doesn't exists , in this case i should get 404 error.