【问题标题】:trouble setting up credentials in Spotipy在 Spotipy 中设置凭据时遇到问题
【发布时间】:2017-08-28 21:49:19
【问题描述】:

我正在尝试运行 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

我做错了什么?我无法在任何地方找到有关如何解决此问题的完整纲要。

提前感谢您的帮助。

【问题讨论】:

  • $ export SPOTIPY_REDIRECT_URI='ocalhost:8888/callback' .... ocalhost?
  • 打错字了,抱歉,已编辑
  • 尝试直接在python中定义凭证:util.prompt_for_user_token(username, scope, client_id, client_secret, redirect_uri)
  • @sKwa 确实给了我提示,谢谢。但我收到这条消息: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. 如果我输入 URL,我会收到 SpotifyOauthError: Bad Request
  • 如何运行脚本?尝试在运行代码之前询问ipython 中的变量:os.getenv('&lt;YOUR VARIABLE&gt;')。可能是您使用不同的终端会话并且未导出变量,即执行export ... -> ipython -> 运行代码。

标签: python spotipy


【解决方案1】:

如果您在使用环境变量时遇到困难,可以尝试改用 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,这样您的密钥就不会包含在存储库中。

【讨论】:

猜你喜欢
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 2021-06-19
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多