# How can I specify a database connection while using "/oauth/token" API for authentication

**URL:** <https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604>\
**Category:** Get Help\
**Created:** [May 17, 2022, 2:21pm UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604 "2022-05-17T14:21:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![junaid](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/junaid/32/35128_2.png) [@junaid](https://community.auth0.com/u/junaid)\
**Post date:** [May 17, 2022, 2:21pm UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604/1 "2022-05-17T14:21:28Z")

</div>

Hi folks, I have two database connections ( one is the default ‘Username-Password-Authentication’ and the other is ‘admin-database’ ) for two sets of users. I have created two different applications “A1” and “A2” (both are of the type “Regular Web Application”) to authenticate them. This is all under one tenant.

“A1” has access to “Username-Password-Authentication” only and “A2” has access to “admin-database” only.

When I use the “signup” API from A2 and create a user in “admin-database”, the user is created.

But when I use the “/oauth/token” API to get an access token, it gives the following error –

```auto
{"error":"invalid_grant","error_description":"Wrong email or password."}

```

Can someone help me understand why?

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [May 17, 2022, 10:35pm UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604/3 "2022-05-17T22:35:24Z")

</div>

Hi @junaid ,

Welcome to the Auth0 Community!

For users created by the “signup” API, they are in the pending status. Could you please verify the user and then try again?

BTW, I tested with POST [/oauth/token](https://auth0.com/docs/api/authentication?http#authorization-code-flow-with-pkce45) endpoint for the “Regular Web Application” and received the access token.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/3/a/3a695c4c4871d1c4f8d2ea2367e4a80eb8953e57.jpeg)

If the error persists, please share the body scripts with me and I will look into this further. Thanks.

---

<div class="post-metadata">

**Author:** ![junaid](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/junaid/32/35128_2.png) [@junaid](https://community.auth0.com/u/junaid)\
**Post date:** [May 18, 2022, 5:10am UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604/4 "2022-05-18T05:10:22Z")

</div>

Hi, thanks for the reply. I am using “grant\_type” as “password”. Below is my request body –

```auto
{
  "username": "*email goes here as it's the username*",
  "password": "*password goes here*",
  "grant_type":"password",
  "audience":"*audience goes here*",
  "scope" : "openid",
  "client_id": "*client ID goes here*",
  "client_secret":"*client secret goes here*"
}

```

I marked the email as verified from the Auth0 dashboard. Still getting the below response –

```auto
{
    "error": "invalid_grant",
    "error_description": "Wrong email or password."
}

```

I have also attached the snapshot of the database connections which have been enabled.

If I disable the “Username-Password-Authentication” connection, the API throws an error that it has been disabled.

If I enable it, then I get the “invalid\_grant” error as shown above.

 ![Screenshot 2022-05-18 at 11.23.44 AM](https://global.discourse-cdn.com/auth0/original/3X/5/6/56c648a63371c099e331778ab364031af05eee43.png)

Please advise.

---

<div class="post-metadata">

**Author:** ![junaid](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/junaid/32/35128_2.png) [@junaid](https://community.auth0.com/u/junaid)\
**Post date:** [May 18, 2022, 6:40am UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604/5 "2022-05-18T06:40:21Z")

</div>

Hi Lihua! I was able to solve this issue on my own.

All I had to do was add Realm support to the API call.

The `grant_type` should `http://auth0.com/oauth/grant-type/password-realm` and the `realm` value should be set to the respective database connection which is `admin-database`.

Below is the complete body for the `/oauth/token` –

```auto
{
    "username": "fill in the username value here. In my case, it is the email",
    "password": "fill in the password here",
    "grant_type": "http://auth0.com/oauth/grant-type/password-realm",
    "audience": "fill in the `audience` value here",
    "scope": "openid",
    "client_id": "fill in the `client_id` value here",
    "client_secret": "fill in the `client_secret` value here",
    "realm": "admin-database"
}

```

The response gave me the `access_token` and `id_token` correctly.

No need to verify the email of the newly-created user.

Thanks for taking the time and replying to my query!

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [May 18, 2022, 4:17pm UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604/6 "2022-05-18T16:17:02Z")

</div>

@junaid ,

Thank you for sharing your solution with our Community! 🙏 👏

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [May 21, 2022, 3:00pm UTC](https://community.auth0.com/t/how-can-i-specify-a-database-connection-while-using-oauth-token-api-for-authentication/83604/8 "2022-05-21T15:00:05Z")

</div>

This topic was automatically closed after 2 days. New replies are no longer allowed.
