spotify-api / spotify-api.js

A complete wrapper for spotify web api for deno, node.js and the browser.
https://spotify-api.js.org
MIT License
211 stars 19 forks source link

auto refreshing token not working #189

Closed adb-sh closed 1 year ago

adb-sh commented 1 year ago

Hey :) i have some problems with the refresh tokens. Not sure if it's a bug or just me doing something wrong. After about one hour form when I started the client I get a "access token expired" error message from the spotify api. I'm creating the spotify client like this: (pseudo code)

const params = new URLSearchParams();
params.append('grant_type', 'authorization_code');
params.append('code', code);
params.append('redirect_uri', store.redirectURL);

const config = {
  headers: {
    'Authorization': 'Basic ' + (new Buffer(store.clientID + ':' + store.clientSecret).toString('base64')),
    'Content-Type': 'application/x-www-form-urlencoded',
  },
};

const tokens = (await axios.post('https://accounts.spotify.com/api/token', params, config))?.data;

await Client.create({
  refreshToken: true,
  retryOnRateLimit: true,
  token: {
    clientID: store.clientID,
    clientSecret: store.clientSecret,
    redirectURL: store.redirectURL,
    refreshToken: tokens.refresh_token,
  },
  async onRefresh() {
    await UserStore.findOneAndUpdate(
      { 'spotify.userId': client.user.id },
      { spotify: { refreshToken: client.refreshMeta.refreshToken } },
    );
  },
});

I also tried to pass code directly without manually requesting a refresh token from the spotify api before. The client acctually stores the the correct refreshToken in client.refreshMeta.refreshToken, but it doesn't automatically requests a new token, when needed apparently.

Originally posted by @adb-sh in https://github.com/spotify-api/spotify-api.js/discussions/14#discussioncomment-3826198

scientific-dev commented 1 year ago

The changes have been update in v9.2.5. If you encounter any bugs, open a new issue to resolve it.

Thanks for your support 👍 !