Basic 認証でもカスタムテーブルにアクセスできますが、よりセキュアな OAuth 認証でカスタムテーブルにアクセスしてみました。
ServiceNow 上でアプリ登録
アプリ登録すると、クライアント ID とクライアントシークレットが発行されます。アプリ登録を行ったユーザーとパスワードも環境変数にセットします。
client_id=9115***********************56bb
client_secret=%60MNp****S%60
snow_username=admin
snow_password=%2FQg%25******Ab
snow_url=https://dev1****5.service-now.com
アクセストークンとリフレッシュトークンを取得
curl $snow_url/oauth_token.do \
-d "grant_type=password" \
-d "client_id=$client_id" \
-d "client_secret=$client_secret" \
-d "username=$snow_username" \
-d "password=$snow_password"
{"access_token":"FML6************s4A","refresh_token":"BltHk*******CjWw","scope":"useraccount","token_type":"Bearer","expires_in":1799}
リフレッシュトークンからアクセストークンを取得
curl $snow_url/oauth_token.do \
-d "grant_type=refresh_token" \
-d "client_id=$client_id" \
-d "client_secret=$client_secret" \
-d "refresh_token=BltHk*******CjWw"
{"access_token":"FML6************s4A","refresh_token":"BltHk*******CjWw","scope":"useraccount","token_type":"Bearer","expires_in":1799}
カスタムテーブルに GET リクエスト
curl $snow_url/api/now/table/u_test \
-H "Authorization: Bearer FML6************s4A"
カスタムテーブルに POST リクエスト
curl $snow_url/api/now/table/u_test \
-H "Authorization: Bearer FML6************s4A" \
-H "Content-Type: application/json" \
-d '{
"u_user_id": "mnrst",
"u_group_id": "infosys"
}'