Open dmmulroy opened 3 months ago
In addition, I would love to see the ability to pass in a AuthenticationStrategy
to SpotifyApi.withAccessToken
so that I don't have to immediately call client.switchAuthenticationStrategy
. My example is that I want to persist my accessToken
to a json file to read on service start.
const client = SpotifyApi.withAccessToken(
config.clientId,
Secret.value(config.clientSecret),
config.accessToken,
// I would like to pass a AuthenticationStrategy here
);
client.switchAuthenticationStrategy(
new ProvidedAccessTokenStrategy(
config.clientId,
Secret.value(config.clientSecret),
config.accessToken,
async (clientId, clientSecret, accessToken) => {
const refreshedToken =
await AccessTokenHelpers.refreshCachedAccessToken(
clientId,
clientSecret,
accessToken,
);
await Bun.write(
"src/do_not_open_on_stream/access-token.json",
JSON.stringify(refreshedToken, null, 2),
).catch(console.error);
return refreshedToken;
},
),
);
ProvidedAccessTokenStrategy
andAccessTokenHelpers.refreshCachedAccessToken
do not properly refresh access tokenThere are multiple problems in the current client on
main
- this is unreleased, I know, but figured I should still file a issue - when trying to refresh access tokens.The![image](https://github.com/spotify/spotify-web-api-ts-sdk/assets/2755722/359abab5-fe1d-4a67-a307-eb433c204734)
refresh token
endpoint requires theAuthorization: Basic base64(client_id:client_secret)
header, despite the docs saying it does not.Because of ☝️, the
ProvidedAccessTokenStrategy
incorrectly implementsrefreshTokenAction
by means ofAccessTokenHelpers.refreshCachedAccessToken
As you can see, the
Authorization
header is missing - I've validated that this request does work when provided the header (I've patched it locally and linked it to my project).The consequence of this, currently, is that you then need to pass the
client_secret
the whole way through the stack to theAccessTokenHelpers.refreshCachedAccessToken
function which is what my patch dos.This leads to the next issue....
The
refreshToken
endpoint response type/docs is wrong or has a bug.This is incorrect as the
refresh_token
field is no returned - this cause a bug in theProvidedAccessTokenStrategy
as the current code assumes therefresh_token
is there.You can see that when
this.refreshToken
is called, theaccessToken
in memory is completely replaced. This then causes the bug to occur next time we attempt to refresh because there is norefresh_token
on theaccess_token