# How to get user metadata from python

**URL:** <https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [August 7, 2023, 11:35am UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017 "2023-08-07T11:35:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrctito](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mrctito/32/49648_2.png) [@mrctito](https://community.auth0.com/u/mrctito)\
**Post date:** [August 7, 2023, 11:35am UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/1 "2023-08-07T11:35:56Z")

</div>

Hello friends, I’m having difficulties fetching user metadata for my Python application. I’ve done a lot of research and tried various suggestions, but either the metadata doesn’t come through, or some error occurs, as in the example below, which raises an exception in the `.client_credentials` command. I appreciate any help.

```python
from auth0.authentication import GetToken
from auth0.management import Auth0

domain = 'mydomain.auth0.com'
client_id = 'myclientid'
client_secret = 'myclientsecret'

get_token = GetToken(domain, client_id, client_secret=client_secret)
token = get_token.client_credentials('https://{}/api/v2/'.format(domain))
mgmt_api_token = token['access_token']
auth0 = Auth0(domain, token['access_token'])

```

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [August 7, 2023, 11:06pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/3 "2023-08-07T23:06:38Z")

</div>

Hi @mrctito,

Welcome to the Auth0 Community!

It looks like you are on the right track with using the Management API to get the user’s user\_metadata. Specifically, you will want to make a request to the [`GET /api/v2/users/{id}`](https://auth0.com/docs/api/management/v2/users/get-users-by-id) endpoint.

Looking at your code snippet, I noticed that the exception being thrown in the `.client_crendentials` command looks to be because the audience is not pointing to your Management API identifier.

It should have the following syntax: `https://yourDomain.region.auth0.com/api/v2`

Could you please make these corrections and see how it goes for you?

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mrctito](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mrctito/32/49648_2.png) [@mrctito](https://community.auth0.com/u/mrctito)\
**Post date:** [August 10, 2023, 12:19pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/4 "2023-08-10T12:19:56Z")

</div>

Thank you!

I solve this issue, but I have another problem related.

I am having trouble understanding and using the API permissions.

To avoid mistakes that I might make, I am using the API TEST option on the Auth0 website, and what I do is take the access\_token and ask for it to be decoded on the [jwt.io](http://jwt.io) website.

The problem is that the permissions of my APIs are never filled.

And I noticed that the same thing happens with the TODO api.

I am using a Regular Web application, and my APIs have the “Enable RBAC” options and “Add Permissions in the Access Tokens” checked.

But I tested the Auth0 Management API with them and things work, and it doesn’t come with the permissions but the SCOPE.

Thank you.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [August 10, 2023, 4:50pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/5 "2023-08-10T16:50:12Z")

</div>

Hi @mrctito,

Thanks for following up.

> [@mrctito](#):
>
> The problem is that the permissions of my APIs are never filled.

Have you made sure that the `audience` parameter that was passed in your login request (`/authorize`) matches the the API that the user was granted permissions?

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mrctito](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mrctito/32/49648_2.png) [@mrctito](https://community.auth0.com/u/mrctito)\
**Post date:** [August 10, 2023, 10:37pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/6 "2023-08-10T22:37:26Z")

</div>

Hi!

Yes, I have. But look, I’m running it through the “Test” option on the dashboard, so I’m not logging in through my application. I am inside the Auth0 website.

I test through the Auth0 site and decode using [jwt.io](http://jwt.io). My application is not involved.

Thank you.

 ![tela 1](https://global.discourse-cdn.com/auth0/original/3X/0/d/0d2c962230c45191781bd8e493a3840c26ef71f2.png)

 ![tela 1-2](https://global.discourse-cdn.com/auth0/original/3X/f/c/fc4f9ee5cbe798552e35fce2aa29344e53734462.png)

 ![tela2](https://global.discourse-cdn.com/auth0/original/3X/b/5/b549e0e7e9e571021d5fc6befeb68727a776115f.png)

 ![tela 3](https://global.discourse-cdn.com/auth0/original/3X/1/4/145832a7d5e0d1887b984f4de2bb49bca7ed0a40.png)

 ![tela 4](https://global.discourse-cdn.com/auth0/original/3X/0/f/0f29ace7ee57b3523553da01d7bc78e73885e4d3.png)

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [August 11, 2023, 4:29pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/7 "2023-08-11T16:29:47Z")

</div>

Hi @mrctito,

Thanks for providing me with this information.

Looking at your screenshots, it seems like you are performing the **Client Credentials** flow instead of the **Authorization Code** flow.

In the Client Credentials flow, this requires no user interaction and is meant for machine-to-machine applications to get an access token for your application.

Whereas the Authorization Code flow requires the user to enter their email and password to log in. Once they have logged in successfully, they will get an access token.

You can verify this information by checking your _response\_pergunta\_ws (Test Application)_ and seeing that it is a machine-to-machine application type. This is also justified by the decoded token, which has the `"gty": 'clilent-credentials'` claim.

If you want to get the user\_metadata in the access token, **you must perform the Authorization Code flow**.

Please refer to our [Call Your API Using the Authorization Code Flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-the-authorization-code-flow) documentation.

To clarify, you should start your login request by calling the following:

```auto
https://{yourDomain}/authorize?
    response_type=code&
    client_id={yourClientId}&
    redirect_uri={https://yourApp/callback}&
    scope={scope}&
    audience={apiAudience}&
    state={state}

```

Please let us know how this goes for you.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mrctito](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mrctito/32/49648_2.png) [@mrctito](https://community.auth0.com/u/mrctito)\
**Post date:** [August 11, 2023, 4:49pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/8 "2023-08-11T16:49:20Z")

</div>

Dear Sir, I am inexperienced in this matter; let me show you how I am doing it. Could you tell me if this is equivalent to what you instructed?

```auto
oauth = OAuth()

oauth.register(
    name='auth0',
    client_id=os.getenv('AUTH0_CLIENT_ID'),
    client_secret=os.getenv('AUTH0_CLIENT_SECRET'),
    client_kwargs={"scope": "openid profile email",},
    server_metadata_url=f"https://{os.getenv('AUTH0_DOMAIN')}/.well-known/openid-configuration",
    access_token_url=f"https://{os.getenv('AUTH0_DOMAIN')}/oauth/token",
    authorize_url=f"https://{os.getenv('AUTH0_DOMAIN')}/authorize",
    api_base_url=f"https://{os.getenv('AUTH0_DOMAIN')}/",
)

@app.get('/login')
async def login(request: Request):
    redirect_uri = request.url_for('auth')
    return await oauth.auth0.authorize_redirect(request, redirect_uri)

```

THANK YOU!

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [August 11, 2023, 6:09pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/9 "2023-08-11T18:09:20Z")

</div>

Hi @mrctito,

Thanks for following up.

Yes, it looks good to me, I see that you are calling the `oauth.auth0.authorize_redirect()` method 👏!

I will share our [Auth0 Python SDK Quickstarts: Add login to your Python Flask app](https://auth0.com/docs/quickstart/webapp/python/interactive) as a reference that you can compare your code with.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![mrctito](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mrctito/32/49648_2.png) [@mrctito](https://community.auth0.com/u/mrctito)\
**Post date:** [August 11, 2023, 8:27pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/10 "2023-08-11T20:27:19Z")

</div>

I appreciate your assistance, but I apologize if I’m having trouble comprehending. Nevertheless, my application still isn’t returning the credentials.

Additionally, one thing I haven’t grasped is how I can test the API using the “Test API” option from the dashboard and view the credentials. Because when I try that approach as I explained initially, the credentials aren’t being provided either.

Thank you.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [August 16, 2023, 3:57pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/11 "2023-08-16T15:57:18Z")

</div>

Hi @mrctito,

Thanks for the reply.

> [@mrctito](#):
>
> my application still isn’t returning the credentials.

It seems like you are still performing the Client Credentials flow using the Dashboard’s Test tab in the API settings. Could you please confirm?

Have you gotten a chance to check that the `response_type=code` in your login request? Try using your browser’s network activity to verify the request parameters.

> [@mrctito](#):
>
> how I can test the API using the “Test API” option from the dashboard and view the credentials.

To reiterate, the “Test API” from the dashboard performs a Client Credentials flow. As mentioned previously, you must perform the Authorization code flow to get the user’s permissions in the access token.

I recommend reviewing the different [grant type flows](https://auth0.com/docs/get-started/applications/application-grant-types). Just to summarize, I recommend using the **Authorization Code** flow to get your user’s permissions in the access token. To do this, you must send the user to the login page with the `oauth.auth0.authorize_redirect( redirect_uri=url_for("callback", _external=True)` command.

Finally, I would urge you to see how our [Python Sample App](https://github.com/auth0-samples/auth0-python-web-app/blob/master/01-Login/server.py) logs the user in.

Alternatively, you could download the Sample App and test it locally to see how it behaves.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [August 30, 2023, 3:57pm UTC](https://community.auth0.com/t/how-to-get-user-metadata-from-python/113017/12 "2023-08-30T15:57:23Z")

</div>

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