3

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?

4
  • 2
    Are you sure you're providing client_id & client_secret in a proper format. Looking at the docs, it suppose to be separated with :. Also try tu run the same flow with curl first and then replicate with python. Commented Dec 18, 2014 at 20:35
  • If you are doing this for educational/recreational purposes then you can ignore this, but there is a Python wrapper for the Spotify Web API here: github.com/plamere/spotipy Commented Dec 19, 2014 at 8:49
  • Otherwise what Mariodev said. The base64 part should be standard_b64encode(client_id + ':' + secret) (no python expert, might not be a correct expression). Commented Dec 19, 2014 at 8:52
  • @mariodev, please put your answer in the Answer section instead of in a comment so that you can get those sweet, sweet, points. Commented Dec 30, 2014 at 10:20

2 Answers 2

5

In spotify api docs it is: Authorization Required. 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)

So i guess you should do:

import base64
'Authorization' : 'Basic ' + base64.standard_b64encode(client_id + ':' + client_secret)

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

Hope it helps!

Sign up to request clarification or add additional context in comments.

1 Comment

What do you use as a redirect_uri that works? All I want to be able to do is make requests. How do I then make an https get request? All their examples give is curl calls which are useless.
0

Are you sure you're providing client_id & client_secret in the proper format? Looking at the docs, it suppose to be separated with :.

Also try to run the same flow with curl first and then replicate with python.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.