使用Spotipy

时间:2015-10-30 20:52:13

标签: python-2.7 oauth-2.0 spotify 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)

我使用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无法识别' 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')

当我尝试这个时,这就是终端中发生的事情:

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标签打开并显示错误 - &#34;无法连接到服务器。&#34; (因为我没有运行服务器。我考虑过在Spotify Web API教程中安装node.js,但我还没有)。然后python脚本要求我复制并粘贴我被重定向到的URL。即使FF无法打开页面,我通过复制整个 URL来实现这一点,包括&#34; code = BG ...&#34;跟随localhost:8888 / callback?我不确定这是一个理想的设置,但至少它是有效的。

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

1 个答案:

答案 0 :(得分:2)

您遵循的过程(包括您的更新)正如示例所示,您不会错过任何内容!显然,这是一个相当简单的教程,但它为您设置了一个令牌,您应该能够获得所需的信息。

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