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

Yup, I agree but this is just for testing in sandbox once after they agreed to give client Id and secret we will generate access token and pass same while posting the details back to resource endpoint. (I hope refresh token is not needed as it is grant_type:client_credentials, correct me if I am wrong).

and my point here is what went wrong with the script when it was successful with rest message test connection by using same access token.



 

@Manikantahere 

ideally we have faced this issue. so we should always generate fresh access token and refresh token for each API call or else what you will have to do is when the refresh token is about to expire you will have to create schedule job and get the new refresh token again

check the links I shared above.

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

@Manikantahere 

Thank you for marking my response as helpful.

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

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

I didn't get any link in your replies.

What is the need of refresh token when third party can't provide it using grant type as client credentials??

you said you faced this issue meaning it was successful using rest message configuration but not with direct script ??


@Manikantahere 

sorry sharing here

refer below links

OAuth2.0: Get new Access Token from existing Refresh Token

How to Setup OAuth2 authentication for outbound RESTMessageV2 integrations

OAuth : Script to Automate Token Request

How to generate the access token once it is expired from Business Rule?

How to get Auth Token using script when grant type is Authorization code

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