Comments (15)
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.
from spotify-web-api-node.
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)
from spotify-web-api-node.
@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?
from spotify-web-api-node.
@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?
from spotify-web-api-node.
from spotify-web-api-node.
@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.
from spotify-web-api-node.
@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.
from spotify-web-api-node.
@brianeroth by any chance, are you passing an expired/invalid access token? The server will return 401 in that case too.
from spotify-web-api-node.
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 }
from spotify-web-api-node.
I have the same problem, regenerated secret, even created new id + secret with another account, same error
from spotify-web-api-node.
@trgwii Thank You!!
from spotify-web-api-node.
A token is now required for the search endpoint. This issue was brought up in the Spotify web API git repo.
from spotify-web-api-node.
got it; thank you
from spotify-web-api-node.
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?
from spotify-web-api-node.
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
from spotify-web-api-node.
Related Issues (20)
- Status of the Project? HOT 9
- When calling getUserPlaylists()
- [WebApiError] Error message type should be a string. HOT 1
- How to fetch more than 100 tracks from a spotify playlist at once using python HOT 1
- Multi user config HOT 1
- PSA: Avoid passing object reference to constructor to avoid state leakage HOT 1
- Missing paMissing parameter in "areFollowingPlaylist" HOT 2
- spotifyApi.setAccessToken is not a function
- Missing GetCurrentUserPlaylists fetch function HOT 1
- Unauthorized when uploading cover image
- I can't Search Track with Korean title
- Outdated Dependencies
- Is there any interest in converting this library to using `fetch`? HOT 4
- How can i get images of a track
- How can i play a track HOT 1
- Server Error 500 HOT 4
- AuthorizationCodeGrant issue HOT 2
- is_local should be an optional property of SpotifyApi.TrackObjectFull
- Invalid authorization code but return accessToken HOT 1
- Can't refresh access token
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spotify-web-api-node.