簡體   English   中英

無法在Spotipy中設置憑據

[英]trouble setting up credentials in 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/user/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

我究竟做錯了什么? 我無法找到如何解決這個問題的簡潔方法。

在此先感謝您的幫助。

如果您正在努力使用環境變量,您可以嘗試使用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所以你的密鑰不包含在回購中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM