- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2024 11:42 PM
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.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2024 12:21 AM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 02:13 AM
@Lior grinberg1 Please check if necessary headers are added to the OAuth profile.
Refer the article below from step no. 17 for OAuth Configuration.
Please mark this as helpful and accept it as a solution if this resolves your query.
Thanks,
Sujatha V.M
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2024 11:55 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2024 12:05 AM
@Lior grinberg1 Please check the OAuth profile defined in Application Registry.
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.
Sujatha V.M.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2024 12:21 AM
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....