diff --git a/FAQ.md b/FAQ.md index 0c144a1..465e8af 100644 --- a/FAQ.md +++ b/FAQ.md @@ -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: diff --git a/examples/app.py b/examples/app.py new file mode 100644 index 0000000..5639f97 --- /dev/null +++ b/examples/app.py @@ -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'