# API authentication using Auth code flow

**URL:** <https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285>\
**Category:** Get Help\
**Tags:** grants, management-api\
**Created:** [May 16, 2024, 1:43pm UTC](https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285 "2024-05-16T13:43:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roy.manotosh](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/roy.manotosh/32/57159_2.png) [@roy.manotosh](https://community.auth0.com/u/roy.manotosh)\
**Post date:** [May 16, 2024, 1:43pm UTC](https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285/1 "2024-05-16T13:43:24Z")

</div>

I am trying to authenticate an API using Auth code flow. I have followed the Hello World React Example along with Hello World Node Express API. The React APP user authentication is working correctly, but when I try to integrate the API call using the React App the access token is not validated by the Auth Server.

I have tested the API using Machine to Machine client credential flow and its working fine. The React App is calling the method “getAccessTokenSilently()” I checked that it is getting a access token but its not accepted by the API server.

Please note the Access Token received through client credential flow shows proper data in JWT validator, but the access token received through Auth code flow seems incorrect. The JWT validator says signature mismatch.

Access token through Client Credential Flow

 ![correct_access_token](https://global.discourse-cdn.com/auth0/original/3X/c/b/cb69224dcfee3a4bb946f3c4324a861df768b90d.jpeg)

Access token through Auth Code Flow

 ![incorrect_access_token](https://global.discourse-cdn.com/auth0/original/3X/8/5/85fbdc8a2b387da62212670f96f1158e03daa201.jpeg)

I am not sure why my protected API is not accessible through Auth code flow.

---

<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:** [May 16, 2024, 7:36pm UTC](https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285/3 "2024-05-16T19:36:27Z")

</div>

Hey there @roy.manotosh welcome to the community!

> [@roy.manotosh](#):
>
> I am not sure why my protected API is not accessible through Auth code flow.

You’ll notice that the access token shared from the auth code flow isn’t decoded - This is due to the fact that the when authenticating via the auth code flow there is no [audience](https://community.auth0.com/t/what-is-the-audience/71414) param being passed and thus the access token returned is [opaque](https://auth0.com/docs/secure/tokens/access-tokens#opaque-access-tokens).

For `auth0-react` in particular you will need to pass an `audience` param as a part of [AuthorizationParams](https://auth0.github.io/auth0-react/interfaces/AuthorizationParams.html#audience) in `Auth0Provider`. Once the audience is included, you should be able to decode the access token returned at [jwt.io](http://jwt.io) and your API should be able to validate it as well.

---

<div class="post-metadata">

**Author:** ![roy.manotosh](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/roy.manotosh/32/57159_2.png) [@roy.manotosh](https://community.auth0.com/u/roy.manotosh)\
**Post date:** [May 17, 2024, 6:08am UTC](https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285/4 "2024-05-17T06:08:15Z")

</div>

Yes, I could figured that out on my own. Thanks a lot for the clarification. The example doc do not mention it clearly. When I went through the Access Token documentation I realised that Audience parameter need to be passed.

It worked. Thanks.

---

<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:** [May 17, 2024, 4:00pm UTC](https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285/5 "2024-05-17T16:00:40Z")

</div>

Great! Glad to hear you got it sorted 🙂

---

<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:** [June 4, 2024, 7:10am UTC](https://community.auth0.com/t/api-authentication-using-auth-code-flow/134285/7 "2024-06-04T07:10:46Z")

</div>

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