spotify / spotify-web-api-ts-sdk

A Typescript SDK for the Spotify Web API with types for returned data.
Other
325 stars 59 forks source link

Error: Bad or expired token #111

Open strenkml opened 3 months ago

strenkml commented 3 months ago

I am making a Discord bot that creates playlists from song links that are post in a Discord channel. I am running into the following error: Error: Bad or expired token. This can happen if the user revoked a token or the access token has expired. You should re-authenticate the user. In the docs it says to use the client credentials if you are making a server side application.

This is the relevant code:

try {
  this.sdk = SpotifyApi.withClientCredentials(this.clientId, this.clientSecret, [
          "playlist-modify-public",
          "user-library-read",
          "playlist-modify-private",
          "user-read-private",
          "user-read-email",
        ]);
} catch (error) {
  console.error(error);
}

const profile = await this.sdk?.currentUser?.profile();

The authentication part runs without error but the profile part throws the error. If I hardcode my user id and try creating the playlist with the following code I just the same error:

const playlist = await this.sdk?.playlists.createPlaylist(userId, {
  name: name,
  collaborative: false,
  description: description,
  public: false,
});
iamrgroot commented 1 month ago

I think you have the same as https://github.com/spotify/spotify-web-api-ts-sdk/issues/70#issuecomment-1712494647

mesrobk commented 1 week ago

Client credentials are used for server-side authentication limited to non-user-related functionality; hence the playlist API is being rejected. It would be great to know if this would be allowed in the future from Spotify.

https://developer.spotify.com/documentation/web-api/tutorials/client-credentials-flow The Client Credentials flow is used in server-to-server authentication. Since this flow does not include authorization, only endpoints that do not access user information can be accessed.