Auth0でカスタムドメインを使います。Management APIにもカスタムドメインでアクセスできました。
はじめに
Auth0でカスタムドメインを使おうとしましたが、Management APIにアクセスする既存コードにも影響がある状況に遭遇しました。 そのため、Management APIにカスタムドメインでアクセスできるか?を試してみました。
まずは、カスタムドメイン無しでAPIアクセスを確認する
Pythonで試していますが、やっていることはシンプルです。アクセストークンを取得して、Management APIにアクセスしています。
sample.py
import json
import requests
DOMAIN = 'your_tenant.auth0.com'
CLIENT_ID = 'your_client_id'
CLIENT_SECRET = 'your_client_secret'
AUDIENCE = 'https://your_tenant.auth0.com/api/v2/'
def main():
token = get_token()
get_users(token)
def get_token():
headers = {'Content-Type': 'application/json'}
url = f'https://{DOMAIN}/oauth/token'
payloads = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'audience': AUDIENCE,
'grant_type': 'client_credentials',
}
res = requests.post(url, headers=headers, data=json.dumps(payloads))
return res.json().get('access_token')
def get_users(token):
headers = {'Authorization': f'Bearer {token}'}
url = f'https://{DOMAIN}/api/v2/users?fields=user_id,email'
response = requests.get(url, headers=headers)
print(json.dumps(response.json(), indent=2))
if __name__ == '__main__':
main()
Management APIにアクセス成功しました。
$ python sample.py
[
{
"email": "xxx",
"user_id": "auth0|yyy"
}
]
カスタムドメインを使って、APIアクセスを確認する
さきほどのコードにあるDOMAIN
部分をカスタムドメインに変更します。
sample.py
DOMAIN = 'your_custom_domain'
なお、Audience
は決まっている値のため、変更しません。
カスタムドメインを使った場合でも、Management APIにアクセス成功しました!
$ python sample.py
[
{
"email": "xxx",
"user_id": "auth0|yyy"
}
]