0

To get an authorization code from the Spotify web api I use this code:

AUTH_URL = 'https://accounts.spotify.com/authorize'
TOKEN_URL = 'https://accounts.spotify.com/api/token'
BASE_URL = 'https://api.spotify.com/v1/'

CLIENT_ID = '******'
CLIENT_SECRET = '*****'

auth_code = requests.get(AUTH_URL, {
    'client_id': CLIENT_ID,
    'response_type': 'code',
    'redirect_uri': 'uri',
    'scope': 'playlist-modify-private',
})

auth_header = base64.urlsafe_b64encode((CLIENT_ID + ':' + CLIENT_SECRET).encode())
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Basic %s' % auth_header.decode('ascii')
}

payload = {
    'grant_type': 'authorization_code',
    'code': auth_code,
    'redirect_uri': 'uri',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
}
access_token_request = requests.post(url=TOKEN_URL, data=payload, headers=headers)
access_token_response_data = access_token_request.json()
access_token = access_token_response_data['access_token']

Only the auth_code for getting the code from Spotify gives <Response 200> instead the code. What could be wrong?

I tried to print the auto code but it gives <Response 200> back. This is ultimately used in the call to Spotify for the token.

Julianr
  • 1
  • 1

1 Answers1

1

Authorization Code Flow in spotify needs a server feature at application side.

It can get code value from spotify server, then request token with code.

In code flow diagram, explain detail here

enter image description here

I will demo with Flask

File save as spotify-client.py

from flask import Flask, request, redirect
from requests_oauthlib import OAuth2Session
from requests.auth import HTTPBasicAuth
import requests
import json

app = Flask(__name__)

AUTH_URL = 'https://accounts.spotify.com/authorize'
TOKEN_URL = 'https://accounts.spotify.com/api/token'
REDIRECT_URI = '<your callback URI>' # my case is 'http://localhost:3000/callback'
CLIENT_ID = "<your client id>"
CLIENT_SECRET = "<your client secret>"
SCOPE = [
    "user-read-email",
    "playlist-read-collaborative"
]

@app.route("/login")
def login():
    spotify = OAuth2Session(CLIENT_ID, scope=SCOPE, redirect_uri=REDIRECT_URI)
    authorization_url, state = spotify.authorization_url(AUTH_URL)
    return redirect(authorization_url)

@app.route("/callback", methods=['GET'])
def callback():
    code = request.args.get('code')
    res = requests.post(TOKEN_URL,
        auth=HTTPBasicAuth(CLIENT_ID, CLIENT_SECRET),
        data={
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': REDIRECT_URI
        })
    return json.dumps(res.json())

if __name__ == '__main__':
    app.run(port=3000,debug=True)

Steps

#1 Run Application

python spotify-client.py

#2 Open Browser and access this URL

http://localhost:3000/login

#3 Login with your Spotify credential This login screen automatically shows to you.

enter image description here

#4 The access-token will shows on your browser if success login.

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14