Spotipy — accessing tracks from a public playlist without authentication

徘徊边缘 提交于 2019-12-08 06:49:37

问题


I want to search through public playlists and get the tracks. So far I have code which can get the names of the playlists but not the tracks:

import spotipy
import sys
sp = spotipy.Spotify()

if len(sys.argv) > 1:
    artist_name = ' '.join(sys.argv[1:])
    results = sp.search(q=artist_name, limit=20, type='playlist')
    for i, t in enumerate(results['playlists']['items']):
        print(i,' ', t['name'])

This will print a list of the first 20 public playlists names given the search condition. What I want is to also print the tracks in each playlist! I thought this would be simple, but after searching it seems like the only way is to via authentication, which I do not want. These tracks are public, so why would I need to authenticate to list the tracks?! There are two reasons I think this. 1) if I add (in the loop):

print t['tracks']

the request response says "This request requires authentication". Additionally, I found this example on the spotipy documentation which is exactly what I want, but only for authenticated users. https://github.com/plamere/spotipy/blob/dd021c4087981b583ef0f2b276cd43bbc6fd429f/examples/user_playlists_contents.py So, is there any way to view the tracks without authenticating as the owner of that playlist? Opening the desktop Spotify app can quickly show anyone that public playlist tracks are completely searchable and viewable so it must be possible. I apologize if this is an extremely specific question -- but I'm not sure where else to ask seeing as this is my first time with this API or with an API like this at all. I have done quite a bit of research on this topic and now have resigned to asking for help.


回答1:


This is a typical OAuth confusion. There are potentially three parties involved here.

  • Your application (that tiny little python snippet above)
  • Spotify Web API
  • A Spotify user

If your app wanted to find and delete a Spotify user's playlists that begin with X, the Spotify Web API would demand that your app first nicely ask the user for permission to do that. Feels natural...

In this scenario, your app Playlist X Deleter first has to authenticate to prove that it actually is Playlist X Deleter. The user then needs to authenticate with Spotify to prove that it actually is the user the Playlist X Deleter wanted to delete playlists for. Then, the user who we now know who it is needs to authorize Playlist X Deleter that we now know who it is to delete playlists.

So, you have an app that authenticates and a user who authenticates.

For information that is public, there is no obvious reason why a user needs to authenticate. There is also no obvious reason why an app needs to authenticate. However, Spotify has decided that the app must authenticate to get public playlist information. Maybe so it can disable bad users who spiders too much playlist data or otherwise abuse the api.

In this case, since there are no private playlists involved, and only read rights, no user needs to authorize anything. In the OAuth world, this is called client credentials flow https://tools.ietf.org/html/rfc6749#section-4.4

Go to the developer console and create an application to get a client_id and client_secret:

https://developer.spotify.com/my-applications/#!/applications/create

Then follow:

https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow

or in your case, supply the client_id and client_secret to spotipy through the SpotifyClientCredentials

doc: http://spotipy.readthedocs.io/en/latest/#spotipy.oauth2.SpotifyClientCredentials

example snippet (that doesn't fill in anything though): https://github.com/plamere/spotipy/blob/master/examples/client_credentials_flow.py



来源:https://stackoverflow.com/questions/38251356/spotipy-accessing-tracks-from-a-public-playlist-without-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!