我正在我的Mac10.10上预装python2.7.10,特别是add_a_saved_track.py,这是从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.sptify.com/my- application注册了应用程序,并收到了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、保密和url作为字符串,就在导入之后,但在util.prompt_for_user_token方法之上。
这导致了追溯:
File "add-track.py", line 8
export SPOTIPY_CLIENT_ID='4f...6'
^
SyntaxError: invalid syntax我注意到文本“Wrangler”并不认为“导出”是一个特殊的词。我在docs.python.org上搜索了“导出”,没有发现任何有用的东西。什么是出口?我怎么会不正确地使用它?
接下来,我尝试将client_id、client_secret和redirect_uri作为参数传递到util.prompt_for_user_token方法中,如下所示:
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/回调。这会导致Firefox选项卡打开时出现错误--“无法连接到服务器。”(因为我没有运行服务器。我考虑过像Spotify Web教程中那样安装node.js,但我还没有)。然后python脚本要求我复制并粘贴我重定向到的URL。即使FF无法打开页面,我还是通过复制整个网址(包括“code=BG.”)来实现这一点。以下是localhost:8888/回调?我不确定这是一个理想的设置,但至少它的工作。
我是否设置了node.js是否重要?
发布于 2017-04-28 10:48:18
您所遵循的过程(包括您的更新)与示例的意图完全相同,您没有遗漏任何东西!显然,这是一个相当简单的教程,但是它为您设置了一个令牌,您应该能够获得所需的信息。
对于凭据,可以通过运行每个导出命令在终端中直接设置这些凭据。在这里阅读更多关于导出的信息:https://www.cyberciti.biz/faq/linux-unix-shell-export-command/
https://stackoverflow.com/questions/33444733
复制相似问题