使用Spotipy设置Spotify凭据 [英] Setting Spotify credentials using Spotipy

查看:302
本文介绍了使用Spotipy设置Spotify凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying out spotipy with python 2.7.10 preinstalled on my mac 10.10, specifically [add_a_saved_track.py][1] Here is the code as copied from 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'

I registered the application with developer.spotify.com/my-applications and received client_id and client_secret. I am a bit unclear about selection of redirect_uri so I set that to 'https://play.spotify.com/collection/songs'

从终端运行此命令时,出现错误消息:

Running this from terminal I get an error that says:

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方法上方.

I put that into my code with the id, secret, and url as strings, just following the imports but above the util.prompt_for_user_token method.

这导致了回溯:

File "add-track.py", line 8
export SPOTIPY_CLIENT_ID='4f...6'
                       ^
SyntaxError: invalid syntax

我注意到Text Wrangler不能将导出"识别为特殊单词.然后我在docs.python.org中搜索"export",但没有任何帮助.什么是出口?我怎么使用不正确?

I noticed that Text Wrangler does not recognize 'export' as a special word. And I searched docs.python.org for 'export' and came up with nothing helpful. What is export? How am I using it incorrectly?

我接下来尝试将client_id,client_secret和redirect_uri作为参数传递给util.prompt_for_user_token方法,如下所示:

I next tried passing the client_id, client_secret, and redirect_uri as arguments in the util.prompt_for_user_token method like so:

util.prompt_for_user_token(username,scope,client_id='4f...6',client_secret='xxx...123',redirect_uri='https://play.spotify.com/collection/songs')

当我尝试这样做时,这就是在终端中发生的情况:

When I tried that, this is what happens in terminal:

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 ,然后得到了以下追溯信息:

I entered https://play.spotify.com/collection/songs and then got this traceback:

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模块.看来我缺少设置客户端凭据的功能.我怎么做?我对此很陌生(如果不是很明显).请帮忙.

It seems like I am missing something, perhaps another part of Spotipy needs to be imported, or some other python module. It seems I am missing the piece that sets client credentials. How do I do that? I am fairly new at this (if that wasn't obvious). Please help.

更新:我将redirect_uri更改为localhost:8888/callback.这将导致Firefox选项卡打开并显示错误-无法连接到服务器". (由于我没有服务器在运行.我曾考虑过像Spotify Web API教程中那样安装node.js,但我还没有.)然后,python脚本要求我复制并粘贴重定向到的URL.即使FF无法打开页面,我也可以通过复制 entire URL(包括本地主机:8888/callback后面的"code = BG ...")来使其正常工作.我不确定这是否是理想的设置,但至少可以正常工作.

UPDATE: I changed redirect_uri to localhost:8888/callback. That causes a Firefox tab to open with an error -- "unable to connect to server." (Since I do not have a server running. I thought about installing node.js as in the Spotify Web API tutorial, but I have not yet). The python script then asks me to copy and paste the URL I was redirected to. Even though FF could not open a page, I got this to work by copying the entire URL including the "code=BG..." that follows localhost:8888/callback? I am not sure this is an ideal setup, but at least it works.

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

Does it matter if I set up node.js or not?

推荐答案

您遵循的过程(包括您的更新)与示例完全相同,并且您不会丢失任何内容!显然,这是一个相当简单的教程,但是它为您设置了令牌,并且您应该能够获得所需的信息.

The process you've followed (including your update) is exactly as the example intends and you are not missing anything! Obviously, it is a fairly simple tutorial, but it sets you up with a token and you should be able to get the information you need.

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

For the credentials, you can set these directly in your Terminal by running each of the export commands. Read more about EXPORT here: https://www.cyberciti.biz/faq/linux-unix-shell-export-command/

这篇关于使用Spotipy设置Spotify凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆