Unable to retrieve username

I am building a sns in django while researching.

I am trying to get the username, but the information I get does not include the username.

I am not sure I understand all the code, is this a problem with the code?

def login(request) :    
    url_params = urlencode({
        'response_type': 'code',
        'client_id': auth0_client_id,
        'redirect_uri': auth0_callback,
        'scope': 'openid profile email',
    })
    
    auth_url = f'https://{auth0_domain}/authorize?{url_params}'
    return redirect(auth_url)
def callback(request) :
    code = request.GET.get('code')
    if code :
        data = {
            'client_id': auth0_client_id,
            'client_secret': auth0_client_secret,
            'redirect_uri': auth0_callback,
            'code': code,
            'grant_type': 'authorization_code',
        }
        url = f'https://{auth0_domain}/oauth/token'
        headers = {'content-type': 'application/json'}
        
        response = requests.post(
            url,
            headers=headers,
            data=json.dumps(data),
        )
        if response.status_code == 200 :
            tokens = response.json()
            access_token = tokens['access_token']
            id_token = tokens['id_token']
            request.session['acess_token'] = access_token
            request.session['id_token'] = id_token
            
            userinfo_response = requests.get(
                f'https://{auth0_domain}/userinfo',
                headers={
                    'authorization': f'Bearer {access_token}'
                }
            )
            userinfo = userinfo_response.json()
            
            management_api_token = get_management_access_token()
            user_id = userinfo['sub'].replace('auth0|', '')
            user_detail_url = f'https://{auth0_domain}/api/v2/users/{user_id}'
            headers = {
                'content-type': 'application.json',
                'Authorization': f'Bearer {management_api_token}'
            }
            
            user_detail_response = requests.get(user_detail_url, headers=headers)
            user_detail = user_detail_response.json()
            userinfo['user_metadata'] = user_detail.get('user_metadata', {})
            
            request.session['userinfo'] = userinfo
            
            print(userinfo)
            return redirect('user:user')
        else :
            print('ユーザー登録に失敗しました…', response.content)
    else :
        return redirect('user:login')

Hi @Koki-1205,

See this topic:

Let me know if you have any questions!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.