The CreatorCon Call for Content is officially open! Get started here.

why i am getting HTTP status 401- Unauthorized??

Manikantahere
Tera Contributor

When I tried with Rest message test connection it is giving me 200 but when I am doing through below script it is throwing me 401? can any one help me in solving out this?

 var body = {"ApproverName":inputs.approver,"UniqueID" : inputs.approvalSysid,"ApproverMail":inputs.approverMail,"RequestItem" :inputs.requestItem,"RequestItemSysId":inputs.itemSysId,"RequestItemName":inputs.title,
  "RequestorMail": inputs.requestorMail,"RequestorName" : inputs.requestorName, "ApprovalType" : "Sequential", "ApplicationName" : "ServiceNow"} ;

  var abc = JSON.stringify(body);
gs.log('test1 - '+abc);
var tokenvalue = gs.getProperty("powerAutomate_request_token");
gs.log("token value is "+tokenvalue);

try{
var power_request = new sn_ws.RESTMessageV2();
power_request.setHttpMethod("post");
power_request.setEndpoint("3rd party end point");
power_request.setRequestHeader("Authorization", "Bearer " +tokenvalue);
power_request.setRequestHeader("Accept","application/json");
power_request.setRequestHeader("Content-Type","application/json");
 
power_request.setRequestBody(abc);

var response = power_request.execute();
var xyz = power_request.getRequestBody();
var httpstatus = response.getStatusCode();

gs.log('ResponseBody - '+xyz);
gs.log("test 4 "+httpstatus);

}
catch(ex){

    var message = ex.message;
    gs.log("test6 "+message);
}


o-auth.png

11 REPLIES 11

Community Alums
Not applicable

Hi @Manikantahere ,

it is Primarily due to invalid credentials (an invalid username or password) or the account not having the ServiceNow permissions required by the connector.

 

Please refer to these threads:

https://www.servicenow.com/community/itsm-forum/getting-http-error-401-unauthorized-while-trying-to-...

https://www.servicenow.com/community/secops-forum/servicenow-rest-api-throwing-error-401-unauthorize...

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Manikantahere 

which endpoint you are consuming?

what type of authentication have you configured?

Seems the token you are using from property at line 8 is not valid and hence giving 401 error means authentication issue

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks for your response Ankur,

As they only have production instance client Id, client secret for posting the data through OAuth2 they denied to give id &secret for time being,  instead they agreed to give us access token while testing which is having span of an hour.

When I tested passing same token in Rest Message as a header and when I click on Test it was showing 200 but throwing me 401 when I tried same with direct code.??

I am consuming post endpoint provided by 3rd party for sending the details.

 

@Manikantahere 

you will have to generate the access token and refresh token using that client id and client secret without which it's not possible.

You can test that access token for 1 hour. but what after that

This is not how integration will work in actual prod instance.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader