0

我正在尝试用 python 和 spotipy 构建一个 Spotify 播放器。我不断收到一条消息,上面写着 INVALID_CLIENT。客户端 ID 连同密码和用户名一起正确输入

import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials

cid ="xx" 
secret = "xx"
username = "xx"

client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

scope = 'user-library-read playlist-read-private'
token = util.prompt_for_user_token(username,scope,client_id='http://localhost:8888/callback/',client_secret='http://localhost:8888/callback/',redirect_uri='http://localhost:8888/callback/')

if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Can't get token for", username)
cache_token = token.get_access_token()

sp = spotipy.Spotify(cache_token)
currentfaves = sp.current_user_top_tracks(limit=20, offset=0, time_range='medium_term')

print(currentfaves)
4

1 回答 1

0

您正在使用客户端凭据和授权代码流。要获得用户的热门曲目,您需要使用授权代码流。

您应该删除客户端凭据行,然后确保util.prompt_for_user_token调用配置正确。在您的代码中,您将所有参数设置为您的重定向 URI,这会给您带来 Invalid Client 错误。似乎您正在关注 Spotipy 文档,这对于此调用不正确。我会尝试做一个PR。

您的代码应如下所示:

import spotipy
import spotipy.util as util

cid ="xx" 
secret = "xx"
username = "xx"

scope = 'user-library-read playlist-read-private'
token = util.prompt_for_user_token(username,scope,client_id=cid,client_secret=secret,redirect_uri='http://localhost:8888/callback/')

if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Can't get token for", username)
cache_token = token.get_access_token()

sp = spotipy.Spotify(cache_token)
currentfaves = sp.current_user_top_tracks(limit=20, offset=0, time_range='medium_term')

print(currentfaves)
于 2018-02-09T12:48:41.693 回答