Announcements

Help Wizard

Step 1

NEXT STEP

unable to refresh token PKCE flow, no error message

unable to refresh token PKCE flow, no error message

My Question or Issue

Hey people, 

 

i am currently developing a react native app using supabase. I am able to sign in using the pkce flow, but i am now stuck at refreshing the spotify token. 

I am receiving an error response but sadly without any further information


{"error": "invalid_request", "error_description": ""}

 

this is my current implementation:



class SpotifyTokenRefreshStrategy implements TokenRefreshStrategy {

async refreshTokens(config: RefreshTokenConfig): Promise<void> {

const url = "https://accounts.spotify.com/api/token"

const clientId: string | undefined =

process.env.EXPO_PUBLIC_SPOTIFY_CLIENT_ID

if (!clientId) {

throw new Error("Spotify client ID not found")

}

const data = {

grant_type: "refresh_token",

refresh_token: config.refreshToken,

client_id: clientId,

}

const body = Object.entries(data)

.map(

([key, value]) =>

`${encodeURIComponent(key)}=${encodeURIComponent(value)}`

)

.join("&")

const payload = {

method: "POST",

headers: {

"Content-Type": "application/x-www-form-urlencoded",

},

body: body,

}

try {

const response = await fetch(url, payload)

console.log("response", await response.json())

if (!response.ok) {

throw new Error(`HTTP error! status: ${response.status}`)

}

const json = await response.json()

console.log("Spotify refresh response:", json)

} catch (error) {

console.error("Error:", error)

throw error

}

}

}

 

the client_id is defined, as well as the refresh token. I can be sure of that because when i remove or modify those parameters from the request, the error message changes to invalid client_id/refresh_token. So it seems that those two paramters are correct, but there is still an issue in the code. 

 

the body strings looks like this:

 

grant_type=refresh_token&refresh_token=AQBtIQoY8tim8s5LMjkgSZBF3lxqdOH_NBL40Ha-VvhRDPb_B04hlwe7s6sw_UPZrGwrwIXmJodBehxeiqZLAqAI4SvJBW54OoP_ooVNmnt704bwCrTQybKtRpFybLq9DN0&client_id=3f122ae12969427e9807f76a43ce6350

 

 

 

i also have added all available scopes, because i thought maybe this is the cause of the error but it isnt.
 

I am really really stuck on this, so i would appreciate any help from y'all 🙂

Reply
1 Reply

Hi. The code looks OK. From what I remember, the refresh token is only sent with the original code exchange. The mistake I did was to overwrite my refresh token with a non-existing refresh token in the response to first refresh. And then subsequent refreshes didn't work.
This is the code I am using: https://github.com/stippi/voice-assistant/blob/11e504b4c5c47a822d52d03b6ab9d3be9bb3a857/src/utils/lo...
Pay especially attention to line 179, where it potentially keeps the original refresh token.
Hope this helps,
Stephan

Suggested posts

Let's introduce ourselves!

Hey there you,   Yeah, you! 😁   Welcome - we're glad you joined the Spotify Community!   While you here, let's have a fun game and get…

ModeratorStaff / Moderator/ 4 years ago  in Social & Random