使用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发布于 2015-05-31 22:16:46
Spotify请求的身份验证类型只是基本的HTTP身份验证。这是一种标准化的身份验证形式,您可以阅读有关here的更多信息。requests库支持基本身份验证,不需要您自己创建标头。有关信息,请参阅python requests documentation。
下面的代码使用请求库身份验证连接到Spotify API。
import requests
client_id = # Enter your client id here
client_secret = # Enter your client secret here
grant_type = 'client_credentials'
#Request based on Client Credentials Flow from https://developer.spotify.com/web-api/authorization-guide/
#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, data=body_params, auth = (client_id, client_secret))
print response我用Spotify创建了一个测试账号,并创建了一个测试客户端id和客户端密码,这就是find。在使用python 2.7.6和请求2.2.1时,我得到了200的响应。
https://stackoverflow.com/questions/30557409
复制相似问题