2017-08-28 79 views
0
设置证书

我试图运行从Spotipy文档一些简单的代码:麻烦Spotipy

scope = 'user-library-read' 

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

token = util.prompt_for_user_token(username, scope) 

我已经尝试过通过运行来建立我的凭证在我的终端(Ubuntu的):

$ export SPOTIPY_CLIENT_ID='[my id]'

$ export SPOTIPY_CLIENT_SECRET='[my secret]'

$ export SPOTIPY_REDIRECT_URI='localhost:8888/callback'

不过,我得到这个:

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' 

      Get your credentials at  
       https://developer.spotify.com/my-applications 

--------------------------------------------------------------------------- 
SpotifyException       Traceback (most recent call last) 
<ipython-input-13-e24370a9caf8> in <module>() 
     7  sys.exit() 
     8 
----> 9 token = util.prompt_for_user_token(username, scope) 

/home/arvcondor/anaconda3/envs/Python3/lib/python3.6/site-packages/spotipy/util.py in prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri) 
    45     https://developer.spotify.com/my-applications 
    46   ''') 
---> 47   raise spotipy.SpotifyException(550, -1, 'no credentials set') 
    48 
    49  sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, 

SpotifyException: http status: 550, code:-1 - no credentials set 

我在做什么错?我一直无法找到如何解决这个问题的简洁流程。

在此先感谢您的帮助。

+0

'$ export SPOTIPY_REDIRECT_URI ='ocalhost:8888/callback'' ....'ocalhost'? –

+0

这是一个错字,对不起,编辑 – snapcrack

+0

尝试直接在python中定义凭证:'util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri)' – sKwa

回答

0

如果你正在努力使用环境瓦尔你可以尝试使用configparser代替:

import configparser 

import spotipy 
import spotipy.oauth2 as oauth2 

config = configparser.ConfigParser() 
config.read('config.cfg') 
client_id = config.get('SPOTIFY', 'CLIENT_ID') 
client_secret = config.get('SPOTIFY', 'CLIENT_SECRET') 


auth = oauth2.SpotifyClientCredentials(
    client_id=client_id, 
    client_secret=client_secret 
) 

token = auth.get_access_token() 
spotify = spotipy.Spotify(auth=token) 

config.cfg应该是这样的:

[SPOTIFY] 
CLIENT_ID=xxxx 
CLIENT_SECRET=xxxx 

如果您使用的是git库添加:

*.cfg 

.gitignore因此您的密钥不包含在回购中。