OAuth2.0 Authorization code flow を使用したリフレッシュトークン取得
OAuth2.0 Authorization code flow を使用したリフレッシュトークン取得のシーケンスを、Salesforceとservicenowで確認する。
(1)以下のシーケンスで、認可コードを取得する。(クリックで拡大)
(2)上記(1)で取得した認可コードを使って、以下のシーケンスでリフレッシュトークンを取得する。(クリックで拡大)
■その他関連リンク
	
    
    OAuth2.0 Authorization code flow を使用したリフレッシュトークン取得のシーケンスを、Salesforceとservicenowで確認する。
(1)以下のシーケンスで、認可コードを取得する。(クリックで拡大)
(2)上記(1)で取得した認可コードを使って、以下のシーケンスでリフレッシュトークンを取得する。(クリックで拡大)
■その他関連リンク
OAuth2.0 Authorization code flow を使って リフレッシュトークンを取得する方法。
(1)servicenow 上でエンドポイントを作成
(1.1)自分の servicenow にログイン
(1.2)System OAuth -> Application Registory
(1.3)Newボタンを押す
(1.4)”Create an OAuth API endopint for external clients” を選択
(1.5)以下の例のように指定してエンドポイントを作成

(2)Get Access code
(2.1)ブラウザを起動
(2.2)以下のURLにアクセスする。
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 (上記(1.5)で指定したリダイレクトURL)
* state ... 123(適当な文字列)

(2.3)以下のようなログインページが表示されるのでログインする(すでにservicenowにログインしている場合は表示されない)

(2.4)”Allow”ボタンを押す。

(2.5)localhostへのリダイレクトは失敗するが、アドレスバーから認可コードを取得できる。

(2.6)認可コードをメモする。
(3)アクセストークンとリフレッシュトークンを取得する。
(3.1)以下のようなシェルを用意する。
#!/bin/sh
MY_INSTANCE="**** YOUR INSTANCE ****"
CLIENT_ID="**** YOUR CLIENT ID ****"
CLIENT_SECRET="**** YOUR CLIENT SECRET ****"
REDIRECT_URI="https%3A%2F%2Flocalhost%2Ftest" # (上記(1.5)で指定したリダイレクトURL)
STATE="123" # 上記(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)上記シェルを、引数に (2.6)でメモした認可コードを指定して実行する。
(3.3)以下のようなjsonを取得できる。その中にリフレッシュトークンが含まれる。
{“access_token”:”***********”,“refresh_token”:”*******”,”scope”:”useraccount”,”token_type”:”Bearer”,”expires_in”:1799}
■リフレッシュトークンを使ってservicenow のREST-APIにアクセスする
以下のリンクを参照。
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 を使って リフレッシュトークンを取得する方法は以下を参照。