如何在 Spotify 中工作,PythonAPI?

How to work in Spotify, Python API?

我已经按照以下方向在我的机器上安装了 Spotipy:https://github.com/plamere/spotipy

我正在尝试 运行 其中一个示例,但没有任何反应,只是文件打开了。这是他们提供的代码:

显示 URN 的艺术家信息或 URL

import spotipy
import sys
import pprint

if len(sys.argv) > 1:
    search_str = sys.argv[1]
else:
    search_str = 'Radiohead'

sp = spotipy.Spotify()
result = sp.search(search_str)
pprint.pprint(result)

当我在 cmd 提示符中键入 "search.py" 时,文件打开。没有其他事情发生。我以为它会打印出与 Radiohead 有关的内容,但没有。

我还需要做其他事情吗?

2017 年 10 月 28 日更新

我已确认请求包已安装。

我收到一堆错误,看起来 client.py 给我错误。它还说我需要提供一个令牌,但此代码不需要令牌。

Python 命令和 Spotipy

的错误

当您尝试访问资源但未通过身份验证时会生成错误 401。 Spotify 目前要求您提供您的凭据您必须在下面注册 link 并在其中创建一个应用程序,最后它将为您提供 Client IDClient Secret,这些值必须放在代码指示的部分。

import sys
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import pprint

if len(sys.argv) > 1:
    search_str = sys.argv[1]
else:
    search_str = 'Radiohead'

client_id = "your_client_id"
client_secret = "your_client_secret"

client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

result = sp.search(search_str)
pprint.pprint(result)