Skip to content

Commit

Permalink
Example to use spotipy in an API, closes #287
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanebruckert committed May 30, 2020
1 parent 816457b commit 587baec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

spotipy can only return fields documented on the Spotify web API https://developer.spotify.com/documentation/web-api/reference/

### How to use spotipy in an API?

Check out [this example Flask app](examples/app.py)

### Incorrect user

Error:
Expand Down
56 changes: 56 additions & 0 deletions examples/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Prerequisites
pip3 install spotipy Flask Flask-Session
export SPOTIPY_CLIENT_ID=client_id_here
export SPOTIPY_CLIENT_SECRET=client_secret_here
// on Windows, use `SET` instead of `export`
Run app.py
python3 -m flask run --port=8080
"""

import os
from flask import Flask, session, request, redirect
from flask_session import Session
import spotipy

app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(64)
app.config['SESSION_TYPE'] = 'filesystem'

Session(app)

auth_manager = spotipy.oauth2.SpotifyOAuth()
spotify = spotipy.Spotify(auth_manager=auth_manager)


@app.route('/')
def index():
if request.args.get("code"):
session['token_info'] = auth_manager.get_access_token(request.args["code"])
return redirect('/')

if not session.get('token_info'):
auth_url = auth_manager.get_authorize_url()
return f'<h2><a href="{auth_url}">Sign in</a></h2>'

return f'<h2>Hi {spotify.me()["display_name"]}, ' \
f'<small><a href="/sign_out">[sign out]<a/></small></h2>' \
f'<a href="/playlists">my playlists</a>'


@app.route('/sign_out')
def sign_out():
session.clear()
return redirect('/')


@app.route('/playlists')
def playlists():
if not session.get('token_info'):
return redirect('/')
else:
return spotify.current_user_playlists()

0 comments on commit 587baec

Please sign in to comment.