Auth0 Home Blog Docs

How can I obtain an id_token from Python code

id_token
python

#1

I am writing a regression test suite in Python, and I would like to run the equivalent to the code below from Python:

> require("auth0-js"], function(auth0)
> {
>     var webAuth = new auth0.WebAuth({
>         domain: 'my_domain', 
>         clientID: 'my_client_id',
>         scope: 'openid offline_access',
>         responseType: 'token id_token',
>     });    
>     webAuth.client.login({
>             realm: 'Username-Password-Authentication',
>             username: 'my_user_email', 
>             password: 'my_password', 
>         }, (err, authResult) => {
>             } else {}
>         }); });

#2

The Auth0.js method in question perform a resource owner password credentials grant (technically an extension grant of this one where you can specify a realm) so the equivalent in Python would be to use the login method in the GetToken class:

https://github.com/auth0/auth0-python/blob/d18417c8e9284f84d5353157a1eaa360e6634a69/auth0/v3/authentication/get_token.py#L123


#3

Thanks @jmangelo
I ended up using this codeā€¦

def get_tokens(user='rod@work.co',
                 password='mypassword',
                 client_id='XXXXXXX',
                 client_secret='YYYYYYYYY',
                 domain='your_tenant.auth0.com'):

    auth0 = GetToken(domain=domain)
    response = auth0.login(client_id,
                           client_secret,
                           user,
                           password,
                           'openid offline_access',
                           'Username-Password-Authentication', # also change this
                           audience='https://your_tenant.auth0.com/api/v2/',
                           grant_type='password')
    return response

#4

Glad it was sorted and thanks for sharing the final code for the benefit of others.


#5