# @auth0/nextjs-auth0 getAccessToken Error

**URL:** <https://community.auth0.com/t/auth0-nextjs-auth0-getaccesstoken-error/106351>\
**Category:** Get Help\
**Tags:** rotating-refresh-tokens, sessions\
**Created:** [May 7, 2023, 7:30pm UTC](https://community.auth0.com/t/auth0-nextjs-auth0-getaccesstoken-error/106351 "2023-05-07T19:30:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xdyer](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@xdyer](https://community.auth0.com/u/xdyer)\
**Post date:** [May 7, 2023, 7:30pm UTC](https://community.auth0.com/t/auth0-nextjs-auth0-getaccesstoken-error/106351/1 "2023-05-07T19:30:01Z")

</div>

Hello,

I am working on implementing some logic into my nextjs endpoints that refreshes an api access token using a refresh token when the current token has expired. Here is a sample of my current code:

```auto
import {
  getAccessToken,
  withApiAuthRequired,
} from '@auth0/nextjs-auth0';
import { NextApiRequest, NextApiResponse } from 'next';
import afterRefresh from '../../../../utils/helpers/afterRefresh';

export default withApiAuthRequired(
  async (req: NextApiRequest, res: NextApiResponse) => {
    const { accessToken } = await getAccessToken(req, res, {
      refresh: true,
      afterRefresh,
    });
    try {
      const { body } = req;
      const result = await fetch(
        `${process.env.API_URI}/invitations/meetings`,
        {
          headers: {
            Authorization: `Bearer ${accessToken}`,
            'Content-Type': 'application/json',
          },
          method: 'POST',
          body: body,
        }
      );
      const data = await result.json();
      return res.status(200).json(data);
    } catch (err) {
      return res.status(500).json({
        error: 'There was an error',
        message: err,
      });
    }
  }
);

```

During the call to getAccessToken() I am getting this error:

> error - [Error [AccessTokenError]: The request to refresh the access token failed. CAUSE: access\_denied (The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined)] {  
> digest: undefined

Any help regarding this error would be very helpful. I am getting a valid refresh token and the NextApiRequest + NextApiResponse objects that are being sent to the getAccessToken function appear to be formed correctly. Thank you!

---

<div class="post-metadata">

**Author:** ![alex.brett](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/alex.brett/32/60157_2.png) [@alex.brett](https://community.auth0.com/u/alex.brett)\
**Post date:** [December 3, 2024, 8:10pm UTC](https://community.auth0.com/t/auth0-nextjs-auth0-getaccesstoken-error/106351/2 "2024-12-03T20:10:19Z")

</div>

Hello @xdyer!

The error message shows that the `req` is not a valid type. Here are some examples of valid calls of the getAccessToken() method.

> <https://github.com/auth0/nextjs-auth0/blob/main/EXAMPLES.md#access-an-external-api-from-an-api-route>

I see that you defined the `req` right before the call as `NextApiRequest`, which may not be one of valid types accepted as shown in the error code. Can you verify that the object being passed is a valid object?

Best,  
Alex

---

<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 30, 2025, 10:52pm UTC](https://community.auth0.com/t/auth0-nextjs-auth0-getaccesstoken-error/106351/3 "2025-01-30T22:52:02Z")

</div>

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