How to implement rest message integration from script after generating token from get oauth token ui action

vidit7
Mega Contributor

Hi All,

I have a requirement to integrate servicenow with a third party tool. So I am using REST with OAuth 2.0 authentication with grant type is resource owner password credentials(password) while implementing it.

Now what I did to implement is =

1. I have made a record in Oauth registry

2. Then created a new rest outbound and selected authentication type as Oauth 2.0 and selected desired oauth profile.

3. Now clicked on get OAuth token and it is asking me to input the user name and password, after filling user name and password, OAuth token is successfully generated.

4. After that i have tested my POST message and got the desired result.

Now the thing is I have to implement the same in script like in business rule or workflow script. So In case of basic authentication, we just call the rest message and get the desired output. In case of Oauth, the token expire in some 30 mins. So after that we are not manually click on get oauth token again and again to generate the token. So I found a script to generate the token and want to use the same token in the rest message to get the output from script.

Can any one of you tell me how to use the token while calling the rest post message if token is not generated from get OAuth token.

Please tell me the process , how to do it?

Thanks & Regards,

Vidit

josh.nerius Please comment.

12 REPLIES 12

Hi Harshvardhan,

 

How to do the REST integration through workflow. We had implemented the REST request through a script include and client script. Setup the REST message. But now the requirement is to do the same thing in workflow? I have no idea of how to do this in a workflow. Can you please let me know the procedure?

vab_13
ServiceNow Employee
ServiceNow Employee

posted same details in Outbound REST web service call using oAuth




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




The specified item was not found.



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


vidit7
Mega Contributor

Hello vab,



link = Outbound REST web service call using oAuth


This is not using the user name and password. So it will generate the token?



link = How to leverage OAuth for Integrations to ServiceNow


Could you please tell me how to write curl scripting in service now.


As per you docx, Access token will be generated when it is expired from the refresh token. So service now will automatically do it or we have to manually do it.


And what if refresh token also got expired?



Please tell me.



Thanks,


Vidit


vab_13
ServiceNow Employee
ServiceNow Employee

If you try following the document, you will understand how it works.


A short description:



1. You register an application with OAuth, here you can configure expire timeframe for refresh and access token.


2. You get the application id and secret key


3. You send a REST call to ServiceNow with Application id, Secret Key, user account Id and password


4. ServiceNow response contains your Refresh and Access tokens


5. You send request with access token to execute your Business Logic in ServiceNow


6. If Access Token is Expired, you need to send a REST request to ServiceNow with refresh token to get a new access token.


7. If your Refresh Token Expires, follow Step#3 again.




You will need to plan around:


1. Validity of your Request Token


2. Validity of your Access Token



And schedule relevant REST Calls from your 3rd Party to make sure tokens get renewed on time.


Read up to get a better gist of it.


Outbound REST web service call using oAuth


How to leverage OAuth for Integrations to ServiceNow



You can leverage a tool called "PostMan" to have a virtual 3rd Party and consume REST API.


For Curl command, here is a good article on it:


curl - Tutorial




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


Vab


vidit7
Mega Contributor

Hi vab,



Thanks for giving me such a big info.



Now can you please tell me how to implement the step no 6 in servicenow - how can I get the access token from refresh token?



How to leverage OAuth for Integrations to ServiceNow



I have gone through the above docx and in this you are using curl with postman. Could you please tell in service now scripting how can we do it? Is curl works in service now.



Let's take it practically = suppose I am using a business rule in my service now instance to call the rest message in which via script I am generating token. Now how can I use that token in my script? and how can I get a token from refresh token?



Thanks,


Vidit