前提条件
- Auth0アカウントの取得
- Auth0にアプリケーションが登録済
- 登録方法はAuth0の導入方法を参照
- アプリケーションにログインできるユーザーが登録済
はじめに
Auth0ではダッシュボード (およびその他) を介して実行できることは全てAPIで実行できるようになっています。
本記事では、APIを利用するための設定と実際の実行例としてユーザー情報の更新方法を紹介します。
APIの利用設定
ここではAuth0のAPIを実行するための設定手順を解説します。
- Auth0のダッシュボードを開き、Applications > APIsから「Auth0 Management API」を選択
- API Explorerタブを選択すると「Create & Authorize Test Application」と表示されているのでクリック
- 表示されるのは初回のみなので画像では表示されていません。
- Applications > Applicationsに「API Explorer Application」が追加されていることを確認
APIを利用してユーザー情報の更新
ここではAuth0のAPIの実行例として、cURLを利用したユーザー情報の更新方法を紹介します。
APIでユーザー情報を更新するには、Access Tokenの取得 ⇨ ユーザー情報を更新の順で実行します。
- 下記のAPIを利用してAccess Tokenを取得
- Domain : API Explorer ApplicationのDomain
- ClientId : API Explorer ApplicationのClient ID
- ClientSecret : API Explorer ApplicationのClient Secret
curl --request POST \
--url 'https://{Domain}/oauth/token' \
--header 'content-type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data 'client_id={ClientId}' \
--data 'client_secret={ClientSecret}' \
--data 'audience=https://{Domain}/api/v2/'
- 例
curl --request POST \
--url 'https://dev-example.jp.auth0.com/oauth/token' \
--header 'content-type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data 'client_id=i2...AU' \
--data 'client_secret=fO...9K' \
--data 'audience=https://dev-example.jp.auth0.com/api/v2/'
- 下記のようなresponseが取得できるため、access_tokenをコピーする
{
"access_token": "ey...gg",
"expires_in": 86400,
"scope": "read:clients create:clients read:client_keys",
"token_type": "Bearer"
}
- 下記のAPIを利用してユーザー情報を更新
- Token : 前述のAPIで取得したaccess_token
- Key : 更新したい項目名
- Value : 更新する値
- Domain : API Explorer ApplicationのDomain
- UserId : 更新したいユーザーのuser_id
curl -H "Authorization: Bearer {Token}" \ -X PATCH -H "Content-Type: application/json" -d '{"Key":Value}' https://{Domain}/api/v2/users/{UserId}
- 例
curl -H "Authorization: Bearer ey...gg" \ -X PATCH -H "Content-Type: application/json" -d '{"name":"newName"}' https://dev-example.jp.auth0.com/api/v2/users/auth0%7C63...f3
- ユーザー情報が更新されていることを確認
終わりに
本記事ではAuth0のAPI利用設定と実行例を紹介しました。
実行可能なAPIの一覧は公式ドキュメントに記載されています。
ドキュメント内では各APIで利用可能な値などが確認できるほか、パラメータを入力することでcURLコマンドの生成などが可能です。
- 閲覧数 1512
画像




コメントを追加