hide's memo
9 Apr, 2023

How to get a refresh token from servicenow

[Japanese(日本語)]

How to get a refresh token from your servicenow instance with using OAuth2.0 Authorization code flow.

(1)Create endpoint on your servicenow.
(1.1)Login your servicenow instance.
(1.2)System OAuth -> Application Registory
(1.3)Push New button.
(1.4)Select “Create an OAuth API endopint for external clients”
(1.5)Apply your endpoint as below.

(2)Get Access code
(2.1)Open web browser
(2.2)Acccess the url below.


https://${YOUR_INSTANCE}.service-now.com/oauth_auth.do?response_type=code&redirect_uri=https%3A%2F%2Flocalhost%2Ftest&client_id=${YOUR_CLIENT_ID}&grant_type=authorization_code&state=123

* redirect_url ... https://localhost/test (you can apply the url that you specified on your endpoint.)
* state ... 123(you can apply your favorit word. this is just a sample)

 

(2.3)you can get login page.(if you don’t login your servicenow instance)


(2.4)Put “Allow” Button.


(2.5)Your redirect must be fail, but you can get the code on your address bar.


(2.6)save the code.

(3)Get Access token and Refresh token.
(3.1)prepare shell script as below.

#!/bin/sh

MY_INSTANCE="**** YOUR INSTANCE ****"
CLIENT_ID="**** YOUR CLIENT ID ****"
CLIENT_SECRET="**** YOUR CLIENT SECRET ****"
REDIRECT_URI="https%3A%2F%2Flocalhost%2Ftest" # you can apply the url that you specified on your endpoint.)
STATE="123" # specify the same word of (2.2)
CODE=$1

RESULT=`curl -X POST https://${MY_INSTANCE}.service-now.com/oauth_token.do -d "grant_type=authorization_code" -d "code=${CODE}" -d "redirect_uri=${REDIRECT_URI}" -d "client_id=${CLIENT_ID}"\
-d "client_secret=${CLIENT_SECRET}" -d "state=${STATE}"`

echo $RESULT

 

(3.2)exec the script above with argument of the code(sequence No.2.6)
(3.3)you can get the refresh token.

{“access_token”:”***********”,“refresh_token”:”*******”,”scope”:”useraccount”,”token_type”:”Bearer”,”expires_in”:1799}

 

■how to access servicenow REST-API with using Refresh token

seek the link below.

Get data from ServiceNow using curl

19 Feb, 2022

Get data from ServiceNow using curl

[japanese(日本語)]

1. Using userid and password

#!/bin/sh

MY_INSTANCE="**** your instance ****"
CLIENT_ID="**** your instance ****"
CLIENT_SECRET="**** your instance ****"
USER="** your userid **"
PASSWD="*** your password ***" # !!warning. you might need to escape it.

#----------------------------------------------
# Get Access token from Id,Passwd
#----------------------------------------------
RESULT=`curl -X POST https://$MY_INSTANCE.service-now.com/oauth_token.do -H "Content-type: application/x-www-form-urlencoded" -d "grant_type=password" -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" -d "username=$USER" -d "password=$PASSWD"`

ACCESS=`echo $RESULT | awk 'BEGIN{FS="access_token\":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}'| sed -s 's/\"//g'`


#----------------------------------------------
#Get Incident with using Access token
#----------------------------------------------
curl -s "https://$MY_INSTANCE.service-now.com/api/now/table/incident?sysparam_query=&sysparam_view=&sysparam_fields=sys_id%2Cshort_description" -H "Authorization: Bearer $ACCESS" -H "Content-Type: application/json"

 

2. Using refresh token

#!/bin/sh

MY_INSTANCE="**** your instance ****"
CLIENT_ID="**** your client id ****"
CLIENT_SECRET="**** your client secret"
REFRESH_TOKEN="****your refresh token ****"

#----------------------------------------------
# Get Access token from Refresh token.
#----------------------------------------------
RESULT=`curl -X POST https://$MY_INSTANCE.service-now.com/oauth_token.do -d "grant_type=refresh_token" -d "scope=useraccount" -d "client_id=$CLIENT_ID" -d "client_secret=$CLIENT_SECRET" -d "refresh_token=$REFRESH_TOKEN"`
ACCESS=`echo $RESULT | awk 'BEGIN{FS="access_token\":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}'| sed -s 's/\"//g'`

#----------------------------------------------
#Get Incident with using Access token.
#----------------------------------------------
curl -s "https://$MY_INSTANCE.service-now.com/api/now/table/incident?sysparam_query=&sysparam_view=&sysparam_fields=sys_id%2Cshort_description" -H "Authorization: Bearer $ACCESS" -H "Content-Type: application/json"

 

3. How to get a refresh token from your servicenow instance.

see the link below.

How to get a refresh token from servicenow