python - 在 Spotipy 中设置凭据时遇到问题

标签 python 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='[我的 ID]'

$ export SPOTIPY_CLIENT_SECRET='[我的 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,这样您的 key 就不会包含在存储库中。

关于python - 在 Spotipy 中设置凭据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927801/

相关文章:

python - 分析数据集的增减

python - 在python中从日期中提取月份

python - 使用加权距离度量的 Scikit-learn 最近邻搜索

python - while 循环中的 Spotipy 请求速度

python - Spotipy - 从轨道名称中获取轨道 ID

python - 在 AWS 上使用 Spotipy 通过 Python 对 Spotify 进行用户身份验证

python - groupby中的pandas聚合函数-默认选项?

python - 如何使用 Python 计算此 CRC?

python - 使 Spotipy 程序变得用户友好

python-3.x - 如何使用该列表作为该函数的参数?