Open andrnors opened 8 years ago
@andrnors The error you are getting means that the access token that you have provided has expired. When this happens, you need to obtain a new access token.
You have an example of a code snippet to refresh a token on the access-token-refresh example.
For more information about what's going on behind the scenes I recommend you to have a look at the authorization flows explained on Spotify's developer site.
@JMPerez I'd like to bring this issue back up. The search endpoint on Spotify doesn't require authorization, so why would this be causing a 401 error?
I'm seeing the same issue on my server after hitting the endpoint a certain number of times. A restart of the server fixes it.
Any thoughts? Thanks.
@brianeroth by any chance, are you passing an expired/invalid access token? The server will return 401 in that case too.
@JMPerez Nope, the only thing that is set on the SpotifyWebApi object is the client id, client secret, and redirect uri. This route doesn't set anything related to access/refresh tokens on the object.
Taking a look at the Spotify documentation for the search endpoint (https://developer.spotify.com/web-api/search-item/), it seems like an access token is needed if searching for a specific market (like US, for example). This isn't something that I'm doing, but is it possible that the library is setting an access token regardless of whether the specific market is included or not?
var SpotifyWebApi = require('spotify-web-api-node');
// I replaced my clientsecret with actual secret
var spotifyApi = new SpotifyWebApi({
clientId : '17f82b582d0b4d2a8461b06f85a86a65',
clientSecret : 'abc..',
});
// Get Elvis' albums
spotifyApi.getArtistAlbums('43ZHCT0cAZBISjO8DG9PnE')
.then(function(data) {
console.log('Artist albums', data.headers);
}, function(err) {
console.error(err);
});
after about 5 or 6 times the above code starts throwing me a 401
😢
{ [WebapiError: Unauthorized] name: 'WebapiError', message: 'Unauthorized', statusCode: 401 }
I have the same problem, regenerated secret, even created new id + secret with another account, same error
I found out the issue: https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/
Spotify removed unauthenticated requests, for me, this approach solved my problem: https://developer.spotify.com/web-api/authorization-guide/#client-credentials-flow
get the access_token property from this request, and use spotifyApi.setAccessToken()
to set it (keep in mind this token HAS an expiry date, so you'll need to call this function again after 3600 seconds have passed; or check the expires_in property)
@trgwii Thank You!!
@JMPerez I'm taking a react-native class which uses fetch to access the Spotify site with the line: https://api.spotify.com/v1/search?q=${ query }&type=artist. Up until last week, this line worked without any problems. now I get a 401 error, why?, is a token now required to access the Spotify site with this line and if so, what is the code I need to use to reach the site so I can continue to work with the react-native class.
Thank you for your help?
A token is now required for the search endpoint. This issue was brought up in the Spotify web API git repo.
got it; thank you
Hi, in May 2017, Spotify disabled unauthenticated calls to the Web API.
Now you need to get and set an access token, to be used in every call to search tracks, etc. The token needs to be set again after it expires in 3600 seconds (1 hour). See example code:
const spotifyApi = new SpotifyWebApi({
clientId: 'myClientId',
clientSecret: 'myClientSecret',
redirectUri: 'myRedirectUri',
});
// Set an access token.
// This is required as Spotify implemented a new auth flow since May 2017.
// See https://developer.spotify.com/news-stories/2017/01/27/removing-unauthenticated-calls-to-the-web-api/
spotifyApi.clientCredentialsGrant()
.then(function(data) {
console.log('The access token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
// Save the access token so that it's used in future calls
spotifyApi.setAccessToken(data.body['access_token']);
}, function(err) {
console.log('Something went wrong when retrieving an access token', err.message);
});
// Continue making other calls to Spotify API as now access token will be sent.
Hey y'all. I'm trying to make a call using the getCategories method and I am using an access token obtained by the clientCredentialsGrant() method (which the console logs that it worked), but I get the 401 error which says I am unauthorized. Is this because I have made multiple requests in the last hour (each time I build it runs the clientCrednetialsGrant method) and now the SpotifyAPI is confused by multiple access tokens having been generated? Or does the access token granted via clientCredentials not allow access to categories?
Hi, whenever I try and refresh the token through the code given in the documentation, I always get the following message:
Could not refresh access token [Error [WebapiError]: Bad Request] { statusCode: 400 }
Could you help me out? I copy the exact code as mentioned in the documenation
When the the server has been running for a while and you call spotifyApi.SearchTrack() it returns { [WebapiError: Unauthorized] name: 'WebapiError', message: 'Unauthorized', statusCode: 401 } It starts working again if the server is restarted. Any idea why this is happening?