【问题标题】:Can anyone tell me why I'm still getting an INVALID_CLIENT message? Spotipy on mac谁能告诉我为什么我仍然收到 INVALID_CLIENT 消息? Mac 上的 Spotipy
【发布时间】:2018-02-08 19:37:43
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: python localhost spotify spotipy


    【解决方案1】:

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

    您应该删除客户端凭据行,然后确保正确配置了 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)
    

    【讨论】:

    • 我这样做了,但我仍然在 Chrome 中收到此错误:无法访问此站点 localhost 拒绝连接。在 Google 上搜索 localhost 8888 回调 ERR_CONNECTION_REFUSED
    • 是的,没关系。那只是因为您实际上并没有在 localhost 上运行服务器。检查您运行 python 脚本的终端 - 它应该提示您输入重定向到的 URL。从 Chrome 的地址栏中获取完整的 URL 并将其粘贴到那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2016-04-25
    • 2016-11-04
    • 2017-10-09
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多