Git Product home page Git Product logo

Comments (15)

eugenesia avatar eugenesia commented on July 30, 2024 34

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.

trgwii avatar trgwii commented on July 30, 2024 6

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.

brianeroth avatar brianeroth commented on July 30, 2024 4

@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.

dcu-sharepoint avatar dcu-sharepoint commented on July 30, 2024 1

@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.

jpiepkow avatar jpiepkow commented on July 30, 2024 1

spotify/web-api#554

from spotify-web-api-node.

JMPerez avatar JMPerez commented on July 30, 2024

@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.

brianeroth avatar brianeroth commented on July 30, 2024

@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.

JMPerez avatar JMPerez commented on July 30, 2024

@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.

joelbandi avatar joelbandi commented on July 30, 2024
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.

trgwii avatar trgwii commented on July 30, 2024

I have the same problem, regenerated secret, even created new id + secret with another account, same error

from spotify-web-api-node.

joelbandi avatar joelbandi commented on July 30, 2024

@trgwii Thank You!!

from spotify-web-api-node.

jpiepkow avatar jpiepkow commented on July 30, 2024

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.

dcu-sharepoint avatar dcu-sharepoint commented on July 30, 2024

got it; thank you

from spotify-web-api-node.

beesmith11 avatar beesmith11 commented on July 30, 2024

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.

uddhav99 avatar uddhav99 commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.