ServiceNowのデータをcurlで取得

[English]

ServiceNowのIncidentを curlで取得するサンプル

1. userid, 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. refresh token を使うパターン

#!/bin/sh

MY_INSTANCE="**** your instance ****"
CLIENT_ID="**** your instance ****"
CLIENT_SECRET="***** your instance ****"
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. リフレッシュトークンを取得する方法

OAuth2.0 Authorization code flow を使って リフレッシュトークンを取得する方法は以下を参照。

Servicenowでリフレッシュトークンを取得する