#Error refreshing access token { error: 'invalid_grant', error_description: 'Refresh token revoked' }

1 messages · Page 1 of 1 (latest)

inland hearth
#

I'm having this kind of error when I'm trying to refresh a token using Authjs. I don't know what's the issue.

This is the function that I'm using to refresh.

async function refreshAccessToken(token: any) {
  const response = await fetch("https://accounts.spotify.com/api/token", {
    method: "POST",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      Authorization: `Basic ${Buffer.from(
        `${process.env.AUTH_SPOTIFY_ID}:${process.env.AUTH_SPOTIFY_SECRET}`
      ).toString("base64")}`,
    },
    body: new URLSearchParams({
      grant_type: "refresh_token",
      refresh_token: token.refresh_token,
    }),
  });
  const refreshedToken = await response.json();
  if (!response.ok) throw refreshedToken;

  console.log("Token refreshed successfully");

  return {
    ...token,
    access_token: refreshedToken.access_token,
    expires_at: Date.now() + refreshedToken.expires_in * 1000,
    refresh_token: refreshedToken.refresh_token || token.refresh_token,
  };
}
forest umbraBOT
#

Hi @inland hearth. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Hmm, I don't know enough to give you a confident answer yet. The knowledge sources provided do not contain specific information about handling refresh token errors with Authjs or Spotify's API.

However, the error message you're seeing typically indicates that the refresh token you're trying to use has been revoked or is no longer valid. This could happen for a number of reasons, such as the token being used too many times, the user's data that the token grants access to has changed, or the token has simply expired.

You might want to check the validity of your refresh token, and ensure that it's being used correctly according to Spotify's API documentation.

For more information on handling tokens and authentication in Next.js, you might find the following resources helpful:

Please note that these links are for general reference and may not directly solve your issue.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: