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,
};
}