在Spotify API上获取授权码(POST请求)

时间:2019-10-01 17:27:58

标签: python post spotify

这是我关注的documentation。在“授权流程”的3个选项中,我正在尝试“ 授权代码流程”。

完成步骤 1。获得您的申请请求授权

卡在步骤 2。让您的应用程序请求刷新并访问令牌

它要求发出一个POST请求,其中包含按照“ OAuth 2.0规范”中定义的“ application / x-www-form-urlencoded”编码的参数:。到目前为止,这是我在有限的知识和Google搜索的基础上所做的事情。

import requests
import base64
from html import unescape 

url = "https://accounts.spotify.com/api/token"

params = {
    "grant_type": "authorization_code",
    "code": <authorization code I got from step 1>,
    "redirect_uri": "http://127.0.0.1:5000/", 
}

headers = {
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",    
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization" : base64.b64encode("{}:{}".format(CLIENT_ID, CLIENT_SECRET).encode('UTF-8')).decode('ascii')
}

html = requests.request('post', url, headers=headers, params=params, data=None) 
print(html.text)

结果,响应码为400

{"error":"invalid_client"}

我应该怎么做才能使其正常工作?我以为我搞定了所有参数。

0 个答案:

没有答案