首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spotify API Post call - Response 415?

Spotify API Post call - Response 415?
EN

Stack Overflow用户
提问于 2015-05-31 20:27:02
回答 1查看 5.4K关注 0票数 3

使用Python,我试图按照链接https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow中客户端凭据流程段落下的说明对Spotify API进行POST调用,这是我想出的代码。

然而,当我运行它时,我得到了Response [415]。有人能告诉我哪里出了问题吗?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-05-31 22:16:46

Spotify请求的身份验证类型只是基本的HTTP身份验证。这是一种标准化的身份验证形式,您可以阅读有关here的更多信息。requests库支持基本身份验证,不需要您自己创建标头。有关信息,请参阅python requests documentation

下面的代码使用请求库身份验证连接到Spotify API。

代码语言:javascript
复制
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的响应。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30557409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档