Deezer’s API with python
--
This article will show you how to use Deezer’s API with python and Flask. We will create a project, see how to authenticate to the API, use some route to retrieves information and play music with the plugin.
Prerequisite
- You need a Deezer account
- Go to https://developers.deezer.com/myapps and create an a new app. You can set http://127.0.0.1:5000/deezer/login in Redirect URL field.
- You will need your Application ID, Secret Key and the Redirect URL to use the API
Set up project
- Create your project
- Create your virtual env
- Install packages
pip install Flask
pip install request
4. Add an app.py file. Add the code below in it, and fill your information in the three variables.
from flask import Flask
app = Flask(__name__)
DEEZER_APP_ID = "MY_APP_ID"
DEEZER_APP_SECRET = "MY_APP_SECRET"
DEEZER_REDIRECT_URI = "http://127.0.0.1:5000/deezer/login"
@app.route('/')
def default():
pass
if __name__ == '__main__':
app.run()
Your project is now ready.
OAuth
You can find the documentation here: developers.deezer.com/api/oauth
We will create two routes for the authentification.
The first one will open a popup if the user is not connected. And you will be redirected to your DEEZER_REDIRECT_URI
with an authorization code in the parameters.
@app.route('/', methods=['GET'])
def default():
url = (f'https://connect.deezer.com/oauth/auth.php?app_id={DEEZER_APP_ID}'
f'&redirect_uri={DEEZER_REDIRECT_URI}&perms=basic_access,email')
return redirect(url)
The second one will request the access token thanks to the authorization code.
# This path should be your redirect url
@app.route('/deezer/login', methods=['GET'])
def deezer_login():
# retrieve the authorization code given in the url
code = request.args.get('code')
# request the access token
url = (f'https://connect.deezer.com/oauth/access_token.php?app_id={DEEZER_APP_ID}'
f'&secret={DEEZER_APP_SECRET}&code={code}&output=json')
response = requests.get(url)
# If it's not a good code we will get this error
if response.text == 'wrong code':
return 'wrong code'
# We have our access token
response = response.json()
return response['access_token']
Let's try this
Start your API with : python app.py
Go to http://127.0.0.1:5000/. A page will open and let you login with your Deezer account. On the next page, your app will ask for permissions. And after accepting, you will end up on your redirect page, with your access token displayed on it.
Use the API and play music
We will go through an example of the use of the API. It will be pretty much the same for each other routes. You can find all in the documentation.
This route, will retrieve all your playlists and give you a json will all the information (id, title, description, list of tracks …)
@app.route('/playlists', methods=['GET'])
def get_playlists():
access_token = request.args.get('token')
response = requests.get(f'https://api.deezer.com/user/me/playlists', {'access_token': access_token})
return response.json()
Keep an ID of one your playlists, we will play it in the next route.
To play music, we have to use the Deezer's plugins. We can customize it here. It’s just an url that you embed in an iframe (or something similar). Our route will take in parameters, the “type_” of content you want to play (can be “playlist”, “album”, “tracks”, “charts” or “favourites”) and the corresponding “id_”.
@app.route('/play', methods=['GET'])
def play():
id_ = request.args.get('id_')
type_ = request.args.get('type_')
url = (f'https://www.deezer.com/plugins/player?'
f'app_id={DEEZER_APP_ID}'
f'&format=classic'
f'&autoplay=true'
f'&playlist=true'
f'&width=700&height=400&color=ff0000'
f'&layout=dark'
f'&size=medium'
f'&type={type_}'
f'&id={id_}'
f'&popup=true'
f'&repeat='
f'0¤t_song_index=0'
f'¤t_song_time=2'
f'&playing=true')
return redirect(url)
For example http://127.0.0.1:5000/play?type_=playlist&id_=YOUR_PLAYLIST_ID
will play your playlist
NB: This is now the best ways and most secure ways of creating an API, and to pass an access token. But for a personal project it will do the job
If you are interested I made a python package (deezersdk) to wrap the API.
Thanks for reading this article. Please let me some comments to improve my future articles, my English, my code or just to say hi.