# Invalid access token nextjs

**URL:** <https://community.auth0.com/t/invalid-access-token-nextjs/104595>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [April 13, 2023, 4:11am UTC](https://community.auth0.com/t/invalid-access-token-nextjs/104595 "2023-04-13T04:11:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dessygil](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dessygil/32/46277_2.png) [@dessygil](https://community.auth0.com/u/dessygil)\
**Post date:** [April 13, 2023, 4:11am UTC](https://community.auth0.com/t/invalid-access-token-nextjs/104595/1 "2023-04-13T04:11:25Z")

</div>

Hi, thanks for any help in advance.

If more detail is needed, I can provide any other information.

I have set up a Nextjs/Django project, and I have followed these tutorials and documentation to do so

1. [Auth0 Next.js SDK Quickstarts: Login](https://auth0.com/docs/quickstart/webapp/nextjs/01-login)

2. [nextjs-auth0/EXAMPLES.md at main · auth0/nextjs-auth0 · GitHub](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route)

What I believe is going wrong is that when I use the nextJS SDK to call getAccessToken, it returns an invalid access token. The reason I think this is because when I input the access token into [jwt.io](http://jwt.io)’s debugger, I get an invalid token, and anytime I try to decode it in my program, it then returns an error. Through this post, I’m hoping to know if I’m following the correct workflow and, if I am, how I can get a valid access token.

I’m not sure I can post my access token here to show as I’m unsure of the security repercussions that might have, but my code to obtain the access token is shown below.

```auto
import { handleAuth } from '@auth0/nextjs-auth0';

export default handleAuth({
    login: handleLogin({
        authorizationParams: {
            audience: process .env.AUTH0_AUDIENCE,
            scope: 'openid profile email'
        }
    })
});

```

```auto
import { getAccessToken, withApiAuthRequired} from '@auth0/nextjs-auth0';

export default withApiAuthRequired(async function products(req, res) {
  const accessToken = await getAccessToken(req, res);
  if (!accessToken) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  console.log(accessToken);
  try {
    const response = await fetch('http://127.0.0.1:8000/private', {
      method: 'GET',
      headers: {
        Authorization: `Bearer ${accessToken}`,
      },
    });
    const data = await response.json();
    return res.status(200).json({ data });
  } catch (error) {
    console.error(error);
    return res.status(500).json({ error: 'Internal Server Error' });
  }
});

```

Im positive this is the correct way to do it, but ANY help would be lovely!

---

<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:** [April 13, 2023, 9:41pm UTC](https://community.auth0.com/t/invalid-access-token-nextjs/104595/3 "2023-04-13T21:41:30Z")

</div>

Hey there @dessygil welcome to the community!

Thanks for the detailed description and code snippets 🙂 Are you positive the `audience` is being passed in the authorize request? When you run your app and attempt to login, you should be able to this param being passed by inspecting the authorize request in the Network tab:

 ![Screenshot 2023-04-13 at 2.34.52 PM](https://global.discourse-cdn.com/auth0/original/3X/5/9/59c8c740387f7ccf2347b208be1a64fce98f53e0.jpeg)

Keep us posted!

---

<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:** [April 29, 2023, 12:16am UTC](https://community.auth0.com/t/invalid-access-token-nextjs/104595/4 "2023-04-29T00:16:39Z")

</div>

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