# Getting Invalid JWT Using getAccessToken method

**URL:** <https://community.auth0.com/t/getting-invalid-jwt-using-getaccesstoken-method/75124>\
**Category:** Get Help\
**Created:** [December 17, 2021, 5:44pm UTC](https://community.auth0.com/t/getting-invalid-jwt-using-getaccesstoken-method/75124 "2021-12-17T17:44:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisvillerbc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/louisvillerbc/32/33874_2.png) [@louisvillerbc](https://community.auth0.com/u/louisvillerbc)\
**Post date:** [December 17, 2021, 5:44pm UTC](https://community.auth0.com/t/getting-invalid-jwt-using-getaccesstoken-method/75124/1 "2021-12-17T17:44:38Z")

</div>

SDK Info: nextjs-auth0  
SDK Version: 1.6.1  
Platform: Node 14.18.0 on Ubuntu 18.04

I am working on a small nextJS web app, and am using the nextJS auth0 library and having trouble authenticating to my external api using the access token returned by the code below (which I have in a file underneath the pages/api directory). It seems like it is returning an auth code…not an auth token as the value generally looks something like “`syyz9m0i-Nkubst3QyCb7TsRBNLVR0_i`”.

I have followed the basic setup steps for nextJS auth0 [here](https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#basic-setup), and have the dynamic routes setup for logging in, etc… I am able to login fine, but when I try to fetch my external api below, I get back `{"code":401,"message":"Jwt is not in the form of Header.Payload.Signature with two dots and 3 sections"}` . Which makes sense given the value that is being returned above…but why is it not returning me an actual valid JWT auth token??

api code:

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

export default withApiAuthRequired(async function families(req, res) {
  // If your Access Token is expired and you have a Refresh Token
  // `getAccessToken` will fetch you a new one using the `refresh_token` grant
  const { accessToken } = await getAccessToken(req, res);
  console.log(accessToken);
  const response = await fetch('https://myprotectedexternalapi.com/endpoint', {
    headers: {
      Authorization: `Bearer ${accessToken}`
    }
  });
  const data = await response.json();
  res.status(200).json(data);
});

```

My calling code to the api above:

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

import useSWR from 'swr';

const fetcher = (url) => fetch(url).then((res) => res.json());

export default withPageAuthRequired(
    function Directory({ user, families }) {
        const { data } = useSWR('/api/families', fetcher);

        return (
            <div>
                <p>THE DATA ({user.email})</p>
                <div>
                    {JSON.stringify(data)}
                </div>
            </div>
        );
    }
);

```

---

<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:** [December 17, 2021, 7:47pm UTC](https://community.auth0.com/t/getting-invalid-jwt-using-getaccesstoken-method/75124/3 "2021-12-17T19:47:17Z")

</div>

Hi @louisvillerbc,

Thanks for reaching out to the Auth0 Community!

I understand you have had issues accessing your API, specifically with an invalid JWT token.

Generally, this happens when the `audience` parameter is not passed as part of the `/authorize` request.

In this case, please take a look at [this related Community topic](https://community.auth0.com/t/auth0-nextjs-auth0-tokens-and-external-api-help/71072/2) which documents the solution.

Please let me know if you have any further questions.

Thank you.

---

<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:** [January 1, 2022, 7:47pm UTC](https://community.auth0.com/t/getting-invalid-jwt-using-getaccesstoken-method/75124/4 "2022-01-01T19:47:53Z")

</div>

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