gpt4 book ai didi

python-2.7 - 使用 Spotipy 设置 Spotify 凭据

转载 作者:行者123 更新时间:2023-12-04 08:48:07 25 4
gpt4 key购买 nike

我正在尝试在我的 mac 10.10 上预装 python 2.7.10 的 spotipy,特别是 [add_a_saved_track.py][1] 这是从 github 复制的代码:

# Add tracks to 'Your Collection' of saved tracks

import pprint
import sys

import spotipy
import spotipy.util as util

scope = 'user-library-modify'

if len(sys.argv) > 2:
username = sys.argv[1]
tids = sys.argv[2:]
else:
print("Usage: %s username track-id ..." % (sys.argv[0],))
sys.exit()

token = util.prompt_for_user_token(username, scope)

if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
results = sp.current_user_saved_tracks_add(tracks=tids)
pprint.pprint(results)
else:
print("Can't get token for", username)

我在 developer.spotify.com/my-applications 注册了应用程序并收到了 client_id 和 client_secret。我对 redirect_uri 的选择有点不清楚,所以我将其设置为 ' https://play.spotify.com/collection/songs '

从终端运行这个我得到一个错误,上面写着:
You need to set your Spotify API credentials. You can do this by
setting environment variables like so:
export SPOTIPY_CLIENT_ID='your-spotify-client-id'
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'
export SPOTIPY_REDIRECT_URI='your-app-redirect-url'

我将 id、secret 和 url 作为字符串放入我的代码中,就在导入之后,但在 util.prompt_for_user_token 方法之上。

这导致了追溯:
File "add-track.py", line 8
export SPOTIPY_CLIENT_ID='4f...6'
^
SyntaxError: invalid syntax

我注意到 Text Wrangler 不会将“导出”识别为特殊词。我在 docs.python.org 中搜索了“export”,但没有找到任何有用的信息。什么是导出?我如何错误地使用它?

接下来我尝试在 util.prompt_for_user_token 方法中将 client_id、client_secret 和 redirect_uri 作为参数传递,如下所示:
util.prompt_for_user_token(username,scope,client_id='4f...6',client_secret='xxx...123',redirect_uri='https://play.spotify.com/collection/songs')

当我尝试这样做时,这就是终端中发生的情况:
User authentication requires interaction with your
web browser. Once you enter your credentials and
give authorization, you will be redirected to
a url. Paste that url you were directed to to
complete the authorization.


Opening https://accounts.spotify.com/authorize?scope=user-library-modify&redirect_uri=https%3A%2F%2Fplay.spotify.com%2Fcollection%2Fsongs&response_type=code&client_id=4f...6 in your browser


Enter the URL you were redirected to:

我输入了 https://play.spotify.com/collection/songs然后得到这个回溯:
Traceback (most recent call last):
File "add-track.py", line 21, in <module>
token = util.prompt_for_user_token(username, scope, client_id='4f...6', client_secret='xxx...123', redirect_uri='https://play.spotify.com/collection/songs')
File "/Library/Python/2.7/site-packages/spotipy/util.py", line 86, in prompt_for_user_token
token_info = sp_oauth.get_access_token(code)
File "/Library/Python/2.7/site-packages/spotipy/oauth2.py", line 210, in get_access_token
raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

好像我遗漏了一些东西,也许需要导入 Spotipy 的另一部分,或者其他一些 python 模块。看来我错过了设置客户端凭据的部分。我怎么做?我对此相当陌生(如果这不明显的话)。请帮忙。

更新:我将 redirect_uri 更改为 localhost:8888/callback。这会导致 Firefox 选项卡打开并显示错误——“无法连接到服务器”。 (因为我没有运行服务器。我想像在 Spotify Web API 教程中那样安装 node.js,但我还没有)。然后,python 脚本要求我复制并粘贴重定向到的 URL。即使 FF 无法打开页面,我还是通过复制整个 URL 来实现这个功能,包括 localhost:8888/callback? 后面的“code=BG...”?我不确定这是一个理想的设置,但至少它可以工作。

我是否设置 node.js 是否重要?

最佳答案

您遵循的过程(包括您的更新)与示例的意图完全相同,您没有遗漏任何东西!显然,这是一个相当简单的教程,但它为您设置了一个 token ,您应该能够获得所需的信息。

对于凭据,您可以通过运行每个导出命令直接在终端中设置这些凭据。在此处阅读有关 EXPORT 的更多信息:https://www.cyberciti.biz/faq/linux-unix-shell-export-command/

关于python-2.7 - 使用 Spotipy 设置 Spotify 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33444733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com