# Getting unauthorized/access\_denied on getting token using refresh token

**URL:** <https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378>\
**Category:** Get Help\
**Tags:** refresh-tokens, nextjs\
**Created:** [August 9, 2022, 2:35am UTC](https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378 "2022-08-09T02:35:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wkjljadlskfjklasjflw](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/wkjljadlskfjklasjflw/32/33224_2.png) [@wkjljadlskfjklasjflw](https://community.auth0.com/u/wkjljadlskfjklasjflw)\
**Post date:** [August 9, 2022, 2:35am UTC](https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378/1 "2022-08-09T02:35:32Z")

</div>

This is a code snippet on my Nextjs project using nextauth to get new tokens followed this [thread](https://next-auth.js.org/tutorials/refresh-token-rotation)

```auto
async function refreshAccessToken(token: any) {
  try {
    const url =
      `${process.env.AUTH0_ISSUER}/oauth/token?` +
      new URLSearchParams({
        client_id: process.env.AUTH0_CLIENT_ID || '',
        client_secret: process.env.AUTH0_CLIENT_SECRET || '',
        grant_type: 'refresh_token',
        refresh_token: token.refreshToken,
        scope: 'offline_access',
      });
    const response = await fetch(url, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
    });

    const refreshedTokens = await response.json();

    if (!response.ok) {
      throw refreshedTokens;
    }

    return {
      ...token,
      accessToken: refreshedTokens.access_token,
      accessTokenExpires: Date.now() + refreshedTokens.expires_in * 1000,
      refreshToken: refreshedTokens.refresh_token ?? token.refreshToken, // Fall back to old refresh token
    };
  } catch (error) {
    return {
      ...token,
      error: 'RefreshAccessTokenError',
    };
  }
}

```

I kept getting `{ error: 'access_denied', error_description: 'Unauthorized' }` no matter how I changed `Application Type`, `Token Endpoint Authentication Method` or other settings.

---

<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:** [August 9, 2022, 4:09pm UTC](https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378/3 "2022-08-09T16:09:09Z")

</div>

Hi @wkjljadlskfjklasjflw ,

Welcome to the Auth0 Community!

I understand that you have encountered the `401 Unauthorized` error when trying to get a new access token using a refresh token.

After testing this myself, I could successfully get a new access token using a refresh token. The only way I could reproduce the `401 Unauthorized` error is when I used an incorrect **client\_id** and/or **client\_secret** in my request.

Given that, could you please double-check that your **client\_id** and **client\_secret** values are correct?

Please let me know your findings.

Thank you.

---

<div class="post-metadata">

**Author:** ![wkjljadlskfjklasjflw](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/wkjljadlskfjklasjflw/32/33224_2.png) [@wkjljadlskfjklasjflw](https://community.auth0.com/u/wkjljadlskfjklasjflw)\
**Post date:** [August 9, 2022, 7:30pm UTC](https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378/4 "2022-08-09T19:30:03Z")

</div>

Hi @rueben.tiow ,

Thanks for the response. Turns out that I have to put all params to request body instead of URLParams, also need to change `Content-Type` to `application/json` and it works.

---

<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:** [August 9, 2022, 11:02pm UTC](https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378/5 "2022-08-09T23:02:15Z")

</div>

Hi @wkjljadlskfjklasjflw,

Thank you for your response, and I’m glad you managed to get the request working!

Please reach out if you have any additional questions.

Thank you.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [December 21, 2022, 4:12pm UTC](https://community.auth0.com/t/getting-unauthorized-access-denied-on-getting-token-using-refresh-token/88378/6 "2022-12-21T16:12:41Z")

</div>


