【问题标题】:How to work in Spotify, Python API?如何在 Spotify、Python API 中工作?
【发布时间】:2017-10-27 03:11:54
【问题描述】:

我已使用以下方向在我的机器上安装了 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 的错误

【问题讨论】:

  • 你有安装 requests 包吗?
  • 当你说你在命令提示符中输入“search.py​​”是什么意思,即“python search.py​​”?
  • 我已经安装了request包。
  • python search.py​​ 不起作用,所以我不得不使用 py search.py​​

标签: python spotify spotipy


【解决方案1】:

当您尝试访问资源但未通过身份验证时,会生成错误 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2023-01-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多