Spotify API{'错误':'INVALID_CLIENT'}授权代码流[400] [英] Spotify API {'error': 'invalid_client'} Authorization Code Flow [400]

查看:24
本文介绍了Spotify API{'错误':'INVALID_CLIENT'}授权代码流[400]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我向https://accounts.spotify.com/api/token发出POST请求的多次尝试之一。

范围设置为‘PlayList-Modify-Public,PlayList-Modify-Private’。

我使用的是Python3.7,Django 2.1.3。

无论我做什么,RESPONSE_DATA都会返回{‘Error’:‘INVALID_CLIENT’}

我尝试了很多方法,包括根据这个特定请求的official Spotify documentation在请求主体中传递客户端id/客户端秘密...无济于事。

请帮帮忙!

def callback(request):

    auth_token = request.GET.get('code')     # from the URL after user has clicked accept
    code_payload = {
        'grant_type': 'authorization_code',
        'code': str(auth_token),
        'redirect_uri': REDIRECT_URI,
    }

    auth_str = '{}:{}'.format(CLIENT_ID, CLIENT_SECRET)
    b64_auth_str = base64.b64encode(auth_str.encode()).decode()

    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic {}'.format(b64_auth_str)
    }

    post_request = requests.post(SPOTIFY_TOKEN_URL, data=code_payload, headers=headers)

    response_data = json.loads(post_request.text)
        # ==> {'error': 'invalid_client'}

推荐答案

我怀疑问题出在Authorization头中的无效字符。尝试使用urlsafe_b64encode而不是b64encode准备该标头值:

b64_auth_str = base64.urlsafe_b64encode(auth_str.encode()).decode()

这篇关于Spotify API{'错误':'INVALID_CLIENT'}授权代码流[400]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆