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

Call a rest api with script and set oauth with setAuthenticationProfile

Lior grinberg1
Tera Contributor

Hello everyone,

I'm encountering an issue while attempting to utilize the setAuthenticationProfile method with OAuth 2.0 in ServiceNow.

I've configured a REST message with the method set to "POST" and authentication type set to "OAuth 2.0". I've also selected a validated profile for authentication.

The code snippet I'm using is as follows:

 

var r = new sn_ws.RESTMessageV2('Test', 'validate');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log("httpStatus " + httpStatus);

This returns a status code of 200, indicating success.

Liorgrinberg1_0-1712990451506.png

However, when I attempt to call the API directly using code, I encounter an issue. Here's the code snippet:

 

var r = new sn_ws.RESTMessageV2();
r.setHttpMethod("POST");
r.setEndpoint("https://testurl.com/executeCommand?accountId=1");
r.setAuthenticationProfile('oauth2', "87f4d130db694250d26fc359139619fc");
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log("httpStatus " + httpStatus);

 

Even though I've provided the OAuth profile's sys_id that I used in the REST message, I receive a status code of 500, indicating an error.

Could anyone provide insight into why my code isn't functioning as expected?

Any assistance would be greatly appreciated.

Best regards,

Lior Grinberg

1 ACCEPTED SOLUTION

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Lior grinberg1 ,

 

I had similar issues, Here i how i fixed it > https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0954749

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

View solution in original post

4 REPLIES 4

Sujatha V M
Kilo Patron
Kilo Patron

@Lior grinberg1  Please check if necessary headers are added to the OAuth profile. 

 

Refer the article below from step no. 17 for OAuth Configuration. 

 

https://www.servicenow.com/community/architect-forum/google-calendar-integration-with-servicenow/td-...

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

HI Sujatha,

I noticed in the HTTP log that in my code the token is not a part of the header.

Do you have any idea why the header doesn't contain the token even if I added the code r.setAuthenticationProfile('oauth2', "87f4d130db694250d26fc359139619fc");?

Best regards,

Lior Grinberg

@Lior grinberg1 Please check the OAuth profile defined in Application Registry. 

 

SujathaVM_0-1713078266949.png

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0779232

 

Please mark this as helpful and accept it as a solution if this resolves your query.

Thanks,

Sujatha V.M.

Please mark this as helpful and accept it as a solution if this resolves your query.
Sujatha V.M.

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Lior grinberg1 ,

 

I had similar issues, Here i how i fixed it > https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0954749

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect