# 401 unauthorized error throws when accessing oauth/token api from Nextjs App

**URL:** <https://community.auth0.com/t/401-unauthorized-error-throws-when-accessing-oauth-token-api-from-nextjs-app/129869>\
**Category:** Get Help\
**Tags:** tenants, management-api\
**Created:** [March 18, 2024, 7:14am UTC](https://community.auth0.com/t/401-unauthorized-error-throws-when-accessing-oauth-token-api-from-nextjs-app/129869 "2024-03-18T07:14:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![glprasannakumar](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@glprasannakumar](https://community.auth0.com/u/glprasannakumar)\
**Post date:** [March 18, 2024, 7:14am UTC](https://community.auth0.com/t/401-unauthorized-error-throws-when-accessing-oauth-token-api-from-nextjs-app/129869/1 "2024-03-18T07:14:34Z")

</div>

Hi Team,

I am using free account of Auth0 to build Sitecore JSS client side application using NextJS and integrate SSO with Auth0 identity provider.  
I am able to call the /authorize api and get the code for my app. And when I am trying get the access token from the code using api oauth/token, I am getting 401 unauthorized issue in my network tab in developer tools.

I tried all the solutions available in the community, but no use.

The below code is the sample code I had written to fetch token.

async function getAccessToken(code: any) {

```
try{

  const token_url = config.baseurl + "/oauth/token?" + 
          new URLSearchParams({
            client_id: config.client_id,
            client_secret: config.client_secret,
            grant_type: 'implicit',
            code: code
          });

  const response = await fetch(token_url, {
    headers: {
      'Content-Type' : 'application/json',
    },
    method: 'POST'
  });

  const token = await response.json();
  console.log(token.access_token);

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

  return {
    accessToken : token.access_token
  };
}
catch(error){
  return {
    error: 'RefreshAccessTokenError',
  };
}

```

};

The below is the error message.

 ![image](https://global.discourse-cdn.com/auth0/original/3X/8/7/8762c27e88c25b0b7a4cad1b83ca76c699f23f68.png)

Please help me to resolve this issue.

---

<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:** [March 19, 2024, 5:31pm UTC](https://community.auth0.com/t/401-unauthorized-error-throws-when-accessing-oauth-token-api-from-nextjs-app/129869/3 "2024-03-19T17:31:29Z")

</div>

Hi @glprasannakumar,

Welcome to the Auth0 Community!

I have reviewed your code snippet and noticed that you’re calling the `/oauth/token` endpoint with `grant_type: 'implicit'`. This is inconsistent with your login flow since you called the `/authorize` endpoint, which returned a code. If that happens, you performed the authorization code flow, which takes the `grant_type: 'authorization_code'`.

With that said, I recommend referring to the documentation below on getting an access token using the authorization code flow:

- [Call Your API Using the Authorization Code Flow](https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow/call-your-api-using-the-authorization-code-flow)

Lastly, if you haven’t already checked out our [Auth0 Quickstarts](https://auth0.com/docs/quickstarts), we have a list of SDKs that we support, including Next.js, which you refer to for an example or download the quickstart for a working sample.

Cheers,  
Rueben

---

<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:** [April 2, 2024, 5:32pm UTC](https://community.auth0.com/t/401-unauthorized-error-throws-when-accessing-oauth-token-api-from-nextjs-app/129869/4 "2024-04-02T17:32:26Z")

</div>

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