Deezer’s API with python

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

  1. Create your project
  2. Create your virtual env
  3. Install packages
pip install Flask
pip install request
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()

OAuth

@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)
# 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

Use the API and play music

@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()
@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&current_song_index=0'
f'&current_song_time=2'
f'&playing=true')

return redirect(url)
One of my playlists

--

--

--

I work at yper. I’m a python developer, learning data science. I’ve made a www.blindfoldchesstactic.com app

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Internationalization Django

Harness Istio Support for Blue/Green and Yellow (Canary Deployments)

Level Up and Launch: Week 7 Review

Top 6 Practices to Deploy Application Performance Testing

AstridDAO: the Astar-native Multi-collateral Stablecoin

This Week I Learned: Windows Subsystem for Linux [2021–12–31]

JAVA-What are the Memory Allocations available in Java?

Trias Weekly Report (Jul 6, 2020 — Jul 12, 2020)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alexis Gomes

Alexis Gomes

I work at yper. I’m a python developer, learning data science. I’ve made a www.blindfoldchesstactic.com app

More from Medium

In operator and having clause in SQL Python

Most Important Python Functions

Python Unit Testing

Unit testing in Python