A python example of the authorization flow in Spotify. This can be used as a template to play around with the APIs. This is not designed for production as the tokens are stored locally in a json file, and the flask web server is development.
- A spotify app.
- Client ID and Client Secret from the app.
- Redirect URI from the app.
- Python
-
Create a .env file and fill out contents with:
CLIENT_IDCLIENT_SECRETREDIRECT_URI
-
Create an empty config.json file
- This will store the token values.
- This file is included in the git ignore to avoid publishing tokens.
-
Create virtual python environment
python3 -m venv venv
-
Enter virtual environment
source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt- Any future dependencies, place in this file.
-
Running tests
pytest
-
Running application
python app.py- Navigate to
localhost:8080or wherever you choose to run inapp.pyto view.
This template has been setup to cover the authorization steps and make it simple to get started on using Spotify APIs.
There is an 'authorize' button which completes the authorization stages. Future clicks of the button once auhtorized will refresh using the refresh token. The official documentation for the authorization stages is here.
The authorization header, with the token, can be retrieved and used for future API requests via songs_backend.py method get_token_headers.
To use an endpoint which requires more permissions, update the scope field found in songs_backend.py method get_auth_params.
An example Spotify API request is found in songs_backend.py method get_liked_songs.
To find and use more API endpoints look here.
Use app.py to enable the front-end to call the backend method and display the contents.