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