Refresh token returns fetch returns Invalid grant_type parameter or parameter missing

Ali

I've setup an API in Next14 App Router to retrieve an access token from the oauth2/token endpoint. I have the client details and refresh token from the Netatmo My App page and I make the call using a server side fetch as...

// route.ts
export const dynamic = "force-static";

export async function GET() {
const payload = JSON.stringify({
grant_type: "refresh_token",
refresh_token: process.env.NETATMO_WEATHER_REFRESH_TOKEN,
client_id: process.env.NETATMO_CLIENT_ID,
client_secret: process.env.NETATMO_CLIENT_SECRET,
});
console.log({ payload });
const res = await fetch("https://api.netatmo.com/oauth2/token", {
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
},
method: "POST",
body: payload,
});
const data = await res.json();

return Response.json({ data });
}

The console outputs the expected values but the fetch return still returns...

{"error":"invalid_request","error_description":"Invalid grant_type parameter or parameter missing"}

 

What am I missing here?

0

Comments

2 comments

  • Comment author
    Leslie Community moderator

    Hello Ali,

    Seems like a simple encoding problem for me 

    Can you try the following code in your Body ? 

    export const dynamic = "force-static";

    export async function GET() {
      const payload = new URLSearchParams({
        grant_type"refresh_token",
        refresh_tokenprocess.env.NETATMO_WEATHER_REFRESH_TOKEN,
        client_idprocess.env.NETATMO_CLIENT_ID,
        client_secretprocess.env.NETATMO_CLIENT_SECRET,
        }).toString();
      console.log({ payload });

      const res = await fetch("https://api.netatmo.com/oauth2/token", {
        headers: {
          "Content-Type""application/x-www-form-urlencoded;charset=UTF-8",
        },
        method"POST",
        bodypayload,
      });
      const data = await res.json();

      return Response.json({ data });
    }

    Have a good day,

    Leslie - Community Manager

    1
  • Comment author
    Ali

    Hi Leslie,

     

    You are an absolute legend - that fixed it thanks so much!

     

    0

Please sign in to leave a comment.