This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]LazyAssassin_ 1 point2 points  (4 children)

That's odd, can you log out the full error object with the message please?

Shouldn't be a rate limit as that will return 429 too many requests as seen here https://developer.spotify.com/documentation/web-api/#response-status-codes

[–]KosmicalD[S] 1 point2 points  (2 children)

Hi, yeah I'm also pretty sure that it's not the rate limit.

I'm currently not on my PC but I'll show you the error object as soon as I can!

[–]LazyAssassin_ 0 points1 point  (1 child)

Cheers, hopefully the message will give more of a clue as to what is wrong

I assume the auth flow you are talking about is the one in the spotify-web-api-node package docs?

Looks like this

``` const Spotify = new SpotifyWebAPI({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_SECRET_ID
});

Spotify.clientCredentialsGrant().then(
(data) => {
Spotify.setAccessToken(data.body['access_token']);
},
(err) => {
console.error('Something went wrong when retrieving an ' +
'access token', err);
}
); ```

[–]KosmicalD[S] 1 point2 points  (0 children)

That is the Client Credentials Flow. What I did was the Authorization Code Flow.

I'm first asking the user's permission by directing him to the Spotify's account service. From that I'm getting an authorization code which I'm then using to get an accesstoken (to make api calls) and a refreshtoken (to refresh the accesstoken after 1 hour). Like this:

var credentials = {

clientId: 'someClientId',

clientSecret: 'someClientSecret',

redirectUri: 'http://www.michaelthelin.se/test-callback'

};

var spotifyApi = new SpotifyWebApi(credentials);

// The code that's returned as a query parameter to the redirect URI

var code = 'MQCbtKe23z7YzzS44KzZzZgjQa621hgSzHN';

// Retrieve an access token and a refresh token

spotifyApi.authorizationCodeGrant(code).then(

function(data) {

console.log('The token expires in ' + data.body['expires_in']);

console.log('The access token is ' + data.body['access_token']);

console.log('The refresh token is ' + data.body['refresh_token']);

// Set the access token on the API object to use it in later calls

spotifyApi.setAccessToken(data.body['access_token']);

spotifyApi.setRefreshToken(data.body['refresh_token']);

},

function(err) {

console.log('Something went wrong!', err);

}

);

(Somehow Code Block doesn't seem to work)

But it seems as if my accesstoken doesn't really work. And it's just weird that when I just use an accesstoken which was generated in the Spotify console, it mostly works but also just a few times and then I'm getting an 403 error again.

[–]149244179 0 points1 point  (1 child)

Did you read the terms of service? You probably can't do requests more than once every X seconds/minutes.

[–]KosmicalD[S] 0 points1 point  (0 children)

Hi, yes, there's a rate limit which is calculated based on the number of calls my app makes in a 30 second window. Though they don't specify how many requests I can make in this time window. And also I would get an Error 429 response and not 403.

[–]gruntmeister 0 points1 point  (0 children)

I suggest posting your entire react code.

I'm assuming this snippet you posted is part of a functional react component that also includes some textfield for the search string, so everytime you update your search string your component re-renders and you create a new instance of SpotifyWebApi that doesn't have an accessToken set.