【发布时间】:2015-08-13 23:06:34
【问题描述】:
使用 Python,我正在尝试按照链接 https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow 上的客户端凭据流段落下的说明对 Spotify API 进行 POST 调用,这是我想出的代码。
但是,当我运行它时,我得到了Response [415]。谁能告诉我怎么了?
import pprint
import requests
import urllib2
import json
import base64
client_id='b040c4e03217489aa647c055265d0ac'
client_secret='2c2928bb7d3e43278424002d2e8bda46b'
authorization_param='Basic ' + base64.standard_b64encode(client_id + ':' + client_secret)
grant_type='client_credentials'
#Request based on Client Credentials Flow from https://developer.spotify.com/web-api/authorization-guide/
#Header must be a base 64 encoded string that contains the client ID and client secret key.
#The field must have the format: Authorization: Basic <base64 encoded client_id:client_secret>
header_params={'Authorization' : authorization_param}
#Request body parameter: grant_type Value: Required. Set it to client_credentials
body_params = {'grant_type' : grant_type}
url='https://accounts.spotify.com/api/token'
response=requests.post(url, header_params, body_params) # POST request takes both headers and body parameters
print response
【问题讨论】:
-
400的状态是你想要的吗?
-
no...400 是错误的请求。我需要 200
-
header_params={'Authorization' : authorization_param,'grant_type' : grant_type}response=requests.post(url, header_params)试试这个 -
不起作用 - 这给了我响应 400....我们想要一个响应 200...
-
您确定您的客户 ID 正确吗?我检查了返回文本,它说客户端 ID 无效。