Spotipy-从公共播放列表访问曲目而无需身份验证 [英] Spotipy -- accessing tracks from a public playlist without authentication

查看:218
本文介绍了Spotipy-从公共播放列表访问曲目而无需身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索公共播放列表并获取曲目.到目前为止,我的代码可以获取播放列表的名称,但不能获取曲目的名称:

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'])

这将在给定搜索条件的情况下打印前20个公共播放列表名称的列表.我还想在每个播放列表中打印曲目!我以为这很简单,但是经过搜索之后,看来唯一的办法就是通过身份验证,这是我所不希望的.这些曲目是公开的,所以为什么我需要进行身份验证才能列出曲目?我认为有两个原因. 1)如果我添加(在循环中):

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']

请求响应显示此请求需要身份验证".另外,我在Spotipy文档中找到了这个示例,正是我想要的,但仅适用于经过身份验证的用户. https://github.com/plamere/spotipy/blob/dd021c408798db43ef0f2/user_playlists_contents.py 因此,是否有任何方法可以查看曲目而不通过身份验证为该播放列表的所有者?打开桌面Spotify应用程序可以快速向任何人显示公共播放列表曲目是完全可搜索和可见的,因此必须有可能. 如果这是一个非常具体的问题,我深表歉意-但我不确定还有什么要问的,因为这是我第一次使用此API或使用此类API.我已经对该主题进行了大量研究,现在已经辞职寻求帮助.

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.

推荐答案

这是典型的OAuth混淆.这里可能涉及三个方面.

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

  • 您的应用程序(上面的那个小小的python代码段)
  • Spotify Web API
  • Spotify用户

如果您的应用要查找和删除以X开头的Spotify用户的播放列表,则Spotify Web API会要求您的应用首先很好地征求用户的许可才能执行此操作.感觉自然...

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

在这种情况下,您的应用程序Playlist X Deleter首先必须进行身份验证,以证明它实际上是Playlist X Deleter.然后,用户需要通过Spotify进行身份验证,以证明它实际上是Playlist X Deleter想要删除其播放列表的用户.然后,我们现在知道是谁的用户需要授权Playlist X Deleter现在知道谁是删除播放列表的人.

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.

对于公开的信息,没有明显的理由说明用户需要进行身份验证.也没有明显的理由说明应用程序需要进行身份验证.但是,Spotify已决定该应用必须进行身份验证才能获取公共播放列表信息.也许这样做可以禁用不良用户,这些不良用户会搜集过多的播放列表数据或滥用api.

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.

在这种情况下,由于不涉及私有播放列表,仅具有读取权限,因此用户无需授权任何内容.在OAuth世界中,这称为客户端凭据流 https://tools.ietf.org/html/rfc6749#section-4.4

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

转到开发者控制台并创建一个应用程序以获取client_id和client_secret:

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

然后关注:

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

或者在您的情况下,通过SpotifyClientCredentials将client_id和client_secret提供给Spotipy

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

示例代码段(尽管未填写任何内容): https ://github.com/plamere/spotipy/blob/master/examples/client_credentials_flow.py

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

这篇关于Spotipy-从公共播放列表访问曲目而无需身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆