问题描述
我想Spotify的API在用户进行身份验证,但它一直返回我的错误codeinvalid_client。我采取了在一个Python Django的解决方案,这是我的code:
I'm trying to authenticate a user in the Spotify API, but it keeps returning me the error code "invalid_client". I'm implementing that on a Python Django solution, that's my code:
headers = {'Authorization': 'Basic '+standard_b64encode(client_id)+standard_b64encode(client_secret)}
r = requests.post('https://accounts.spotify.com/api/token', {'code': code, 'redirect_uri': redirect_uri, 'grant_type': grant_type, 'headers': headers}).json()
任何想法,为什么它不工作?
Any idea why it's not working?
推荐答案
在Spotify的API文档,它是:
授权
需要。基地64恩包含客户端ID和客户端密钥codeD字符串。该字段的格式必须为:授权:基本的base64 EN codeD(CLIENT_ID:client_secret)
In spotify api docs it is:AuthorizationRequired. 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)
所以我想你应该做的:
import base64
'Authorization' : 'Basic ' + base64.standard_b64encode(client_id + ':' + client_secret)
它的工作对我来说这样试试。如果它不工作,我的code是:
It's working for me so try it. If it doesn't work my code is:
@staticmethod
def loginCallback(request_handler, code):
url = 'https://accounts.spotify.com/api/token'
authorization = base64.standard_b64encode(Spotify.client_id + ':' + Spotify.client_secret)
headers = {
'Authorization' : 'Basic ' + authorization
}
data = {
'grant_type' : 'authorization_code',
'code' : code,
'redirect_uri' : Spotify.redirect_uri
}
data_encoded = urllib.urlencode(data)
req = urllib2.Request(url, data_encoded, headers)
try:
response = urllib2.urlopen(req, timeout=30).read()
response_dict = json.loads(response)
Spotify.saveLoginCallback(request_handler, response_dict)
return
except urllib2.HTTPError as e:
return e
希望它帮助!
这篇关于与Python Spotify的API认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!