Ste*_*eve 8 spotify oauth-2.0 python-2.7 spotipy
我正在试用我的mac 10.10预装的python 2.7.10,特别是[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)
Run Code Online (Sandbox Code Playgroud)
我使用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'
Run Code Online (Sandbox Code Playgroud)
我把它放在我的代码中,id,secret和url作为字符串,只是在导入之后但在util.prompt_for_user_token方法之上.
这引起了追溯:
File "add-track.py", line 8
export SPOTIPY_CLIENT_ID='4f...6'
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我注意到Text Wrangler不会将'export'识别为特殊字.我在docs.python.org上搜索了"export",但没有提供任何帮助.什么是出口?我是如何错误地使用它的?
我接下来尝试将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')
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,这就是终端中发生的事情:
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:
Run Code Online (Sandbox Code Playgroud)
我输入了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
Run Code Online (Sandbox Code Playgroud)
好像我遗漏了一些东西,可能是Spotipy的另一部分需要导入,或者其他一些python模块.似乎我错过了设置客户端凭据的部分.我怎么做?我对此很新(如果这不是很明显).请帮忙.
更新:我将redirect_uri更改为localhost:8888/callback.这导致Firefox标签打开时出现错误 - "无法连接到服务器".(因为我没有运行服务器.我考虑过在Spotify Web API教程中安装node.js,但我还没有).然后python脚本要求我复制并粘贴我被重定向到的URL.即使FF无法打开页面,我通过复制包含localhost:8888/callback之后的"code = BG ..." 的整个 URL 来实现这一点.我不确定这是一个理想的设置,但至少它是有效的.
如果我设置node.js是否重要?
小智 3
您所遵循的过程(包括您的更新)与示例所示完全一致,并且您没有遗漏任何内容!显然,这是一个相当简单的教程,但它为您设置了一个令牌,您应该能够获取所需的信息。
对于凭据,您可以通过运行每个导出命令直接在终端中设置它们。在这里阅读有关 EXPORT 的更多信息:https://www.cyberciti.biz/faq/linux-unix-shell-export-command/
归档时间: |
|
查看次数: |
4373 次 |
最近记录: |