# My api is throwing 401 unauthorized when I log in

**URL:** <https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157>\
**Category:** Get Help\
**Tags:** classic-universal-login-experience, login-experience\
**Created:** [October 10, 2023, 8:07am UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157 "2023-10-10T08:07:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sellochrismodise](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sellochrismodise/32/51025_2.png) [@sellochrismodise](https://community.auth0.com/u/sellochrismodise)\
**Post date:** [October 10, 2023, 8:07am UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157/1 "2023-10-10T08:07:28Z")

</div>

HI When I am able to login but when I call the api using my access token i get a 401 unauthorized and the message says Bearer error=“invalid\_token”.  
Below are images showing my setup.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/7/3/7342364dd51aa1505e19c9b30b3f3d404645eeec.png)

 ![image](https://global.discourse-cdn.com/auth0/original/3X/f/9/f9bd0f7a753c1f14b5b812dedf1ebd7e03ea4f2e.png)

![image](https://global.discourse-cdn.com/auth0/original/3X/c/5/c5bd19ea08ec0c8c36d2aae5b8e2f1805d43568b.png)

Hi I hope you can see the images

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 10, 2023, 6:16pm UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157/3 "2023-10-10T18:16:11Z")

</div>

Hey @sellochrismodise !

Good to know you’re able to log in and get token(s) - Thanks for sharing those images 🙂

- What does your token look like when you decode it at [jwt.io](http://jwt.io)? Do you mind sharing the decoded token (sensitive info redacted) here?

- Do you have the audience `http://localhost:5000` [registered as an API](https://auth0.com/docs/get-started/auth0-overview/set-up-apis) in Auth0?

---

<div class="post-metadata">

**Author:** ![sellochrismodise](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sellochrismodise/32/51025_2.png) [@sellochrismodise](https://community.auth0.com/u/sellochrismodise)\
**Post date:** [October 11, 2023, 5:42am UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157/4 "2023-10-11T05:42:50Z")

</div>

Hi tyf yes that is the identifire of my api ap on auth0.

here is one of my access tokens:  
eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIiwiaXNzIjoiaHR0cHM6Ly9jcGQtdGVuYW50LnVzLmF1dGgwLmNvbS8ifQ…vtY5OEu0kLQ\_QCUd.7weF4a-7fgGTZ9-VPLLDMSQNmWy\_KOt8Tqyhaaz9f-SVqfeDOa7LmLx8aQGYXNHnjoNR\_BN9P9VDGI5Qch3PjYA8dSGbVfqO0sRJfVHGX8PsUnEHRD3Ak6KAfmFw7v-8-V5O2FJCSQXuTqZFt4AW\_iMYTt2d6PXred4uxp\_Yjfx8JfBaXbi5sDbP4-Q5-w.Q4wmKgszZM9yzh2-sKaiuw

Here is what the debugger says

 ![image](https://global.discourse-cdn.com/auth0/original/3X/6/d/6d5f46c800e6ecdbab65bcb6ee1afb47f47d0786.png)

---

<div class="post-metadata">

**Author:** ![sellochrismodise](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sellochrismodise/32/51025_2.png) [@sellochrismodise](https://community.auth0.com/u/sellochrismodise)\
**Post date:** [October 11, 2023, 7:56pm UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157/5 "2023-10-11T19:56:50Z")

</div>

Hi tyf

I had a look around and it seems like the audience was not been set correctly based on my config file. but now i get a correct access token that can get access to my api endpoints.  
Heres what i did for anyone who might struggle with the same issue.

![image](https://global.discourse-cdn.com/auth0/original/3X/b/8/b8fb0bf2130bcb4dc8130d7acac91534a7da3660.png)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [October 11, 2023, 11:48pm UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157/6 "2023-10-11T23:48:22Z")

</div>

Hey @sellochrismodise glad you were able to get this sorted!

> [@sellochrismodise](#):
>
> I had a look around and it seems like the audience was not been set correctly based on my config file.

That’ll do it! Without an `audience` param you were getting back an [opaque token](https://community.auth0.com/t/why-is-my-access-token-not-a-jwt-opaque-token/31028) which can only be used against the `/userinfo` endpoint.

---

<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:** [October 25, 2023, 11:49pm UTC](https://community.auth0.com/t/my-api-is-throwing-401-unauthorized-when-i-log-in/118157/7 "2023-10-25T23:49:13Z")

</div>

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