Outbound REST web service call using oAuth

dmolnar
Giga Contributor

//authentication type ='basic'/ 'oauth2'

//This line below is optional if you have configured OAuth as authentication type in your outbound REST

r.setAuthentication('oauth2', 'OAuth_Client1');

 

What is OAuth_Client1 referring to?  

I have tried the OAuth Profile name and  oauth_entity name, never worked.

Does anyone know?

1 ACCEPTED SOLUTION

dmolnar
Giga Contributor

This is useful thank everyone. But I was looking for the syntax and how to use it. What I found was the UI Action - "Preview Script". But I did not realize this is no longer used in Geneva+. This was to override the security profile setup in the REST method. The issue is in Geneva (maybe Fuji) and above, this is not used anymore. But I found the answer, was hard to find. Here it is:



Old way:


//override authentication profile


//authentication type ='basic'/ 'oauth2'


//r.setAuthentication(authentication type, profile name);


r.setAuthentication('oauth2', 'OAuth_Client1');



New way:


The new syntax is RESTMessageV2 - setAuthenticationProfile(String type, String profileId)


You can find the details here: RESTMessageV2 - setAuthenticationProfile(String type, String profileId)



NameTypeDescription
typeStringThe type of authentication profile to use. Valid values are 'basic' to use basic authentication, or 'oauth2' to use OAuth 2.0.
profileIdStringThe sys_id of an authentication profile record. When using basic auth, specify the sys_id of a Basic Auth Configuration [sys_auth_profile_basic] record. When using OAuth 2.0, specify the sys_id of a OAuth Entity Profile [oauth_entity_profile] record.

View solution in original post

6 REPLIES 6

vab_13
ServiceNow Employee
ServiceNow Employee

I wrote this Doc a while back, have a read and you should be able to leverage OAuth for REST after this.



How to leverage OAuth for Integrations to ServiceNow



Summary:



STEP#1: under "System OAuth" -> "Application Registry" => Configure the application in ServiceNow instance: Create application


Make a note of:


<client_id> AND <client_secret>



STEP#2:   Request Access Token


Use the OAuth 2.0 API to construct an access token request.


Endpoint URL: <Your Instance>.service-now.com/oauth_token.do


Request format: All requests should format as a URL-encoded HTTP POST body.


Request parameters: Most request parameters are required.


Response format: JSON


Response parameters: The response contains one or more OAuth tokens.




REST Call Parameters:


grant_type:password


client_id:74c25ae08407a6002716dacdf5ac09e3


client_secret:test


username:admin


password:admin



Above Token request can also be done directly via Curl:


$ curl -d"grant_type=password&client_id=74c25ae08407a6002716dacdf5ac09e3&client_secret=test&username=admin&password=admin"


https://vabistanbul.service-now.com/oauth_token.do





3. Make a TEST REST Request with an OAuth Token:


- Token allows ServiceNow to identify which user is sending the request.


- All ACL applying to this user also apply to these requests.


- We will try to fetch one Incident record by using OAuth access Token:



REST Request will look like:



GET: https://vabistanbul.servicenow.


com/api/now/table/incident?sysparm_limit=1


accept:application/json


Authorization:Bearer


jsFh2_X3IxGJjm5D5hIllaufm0T9su6cjrJqdRgcx4lOQev8Ee0npP0NFTTyth0UUtn


Aabt89slgfvzky4FbMA


Note: ( Authorization:Bearer +Token string)




Curl:


curl -H "Accept:application/json" -H "Authorization:Bearer


jsFh2_X3IxGJjm5D5hIllaufm0T9su6cjrJqdRgcx4lOQev8Ee0npP0NFTT


yth0UUtnAabt89slgfvzky4FbMA" "https://vabistanbul.servicenow.com/api/now/table/incident?sysparm_limit=1




Hope this helps.



Docs:


OAuth 2.0 tutorial - create an OAuth provider and profile




Mark your feedback( Like or Helpful or Correct) as per the impact of my response. Cheers!


Vab


vab_13
ServiceNow Employee
ServiceNow Employee

The Document I wrote a while back, have a read and you should be able to leverage OAuth for REST after this.



How to leverage OAuth for Integrations to ServiceNow





Mark your feedback( Like or Helpful or Correct) as per the impact of my response. Cheers!



Vab


Aditya Telideva
ServiceNow Employee
ServiceNow Employee

Please do refer to the article How to Setup OAuth2 authentication for RESTMessageV2 integrations.


I personally found this very helpful


Best,


Adi