Skip to main content

Another client for the Spotify Web API

Project description

Another wrapper for the Spotify Web API, built on top of requests and requests_oauthlib.

I wrote this package as a kind of “code generation experiment”: the code (and doc) of Spotify class was almost entirely generated using a script that scrapes the official Spotify Web API documentation.

For scripts and desktop apps, this package (optionally) includes a flask server (meant to be run locally) to handle user authorization without requiring the user to manually copy and paste the OAuth2 callback URL (containing the token) from the browser to your app.

  • Free software: MIT license

Installation

If you don’t need the authorization flask app:

pip install spotipie

otherwise:

pip install spotipie[auth-app]

Usage

  1. Obtain your credentials as described here.

  2. (Optional) Store your credentials and redirection URI as environment variables:

    • SPOTIPIE_CLIENT_ID

    • SPOTIPIE_CLIENT_SECRET

    • SPOTIPIE_REDIRECT_URI;

    you could also use another prefix, SPOTIPIE is just the default one.

  3. To use the spotipie.Spotify client you first need to create an HTTP session and authenticate it. The Spotify constructor takes whatever behaves like a requests.Session. spotipie provides one session class for each of the three OAuth2 authorization flows supported by the Spotify API (see Authorization Flows); these classes are built on top of requests_oauthlib.OAuth2Session (by composition, not inheritance):

    • ClientCredentialsSession

    • AuthorizationCodeSession

    • ImplicitGrantSession

    To see how to create a session, see the Examples.

  4. Once you have an authenticated session, you can wrap it with the client and you’re ready to make any API call you want:

    spotify = Spotify(session)
    results = spotify.search('symphony', obj_type='playlist')

    See the API of the client here.

What OAuth2 flow should I use?

A backend web application should use:

  • the client credentials flow if it doesn’t need access to private user data;

  • the authorization code flow otherwise.

For scripts and desktop application… it’s more complicated. The recommended flow in this case is “Authorization code with PKCE” but it’s not supported by Spotify at the time I’m writing this.

It’s not recommended to distribute your code with your API secret key in it, so both the client credentials flow and the authorization code flow should not be used, unless you ask your users to use their own API keys; this can be acceptable if your target users are other developers.

The implicit grant flow was designed for apps that run in the browser but has been used for “native apps” too since it doesn’t need the client secret key; unfortunately, for native apps, it’s neither very safe nor convenient from a user perspective since the authorization is not refreshable.

Examples

All the examples assume your API credentials and redirect URI are stored as environment variables.

API Reference

https://spotipie.readthedocs.io/en/latest/api/spotipie.html

Changelog

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spotipie-0.1.2.tar.gz (46.9 kB view details)

Uploaded Source

Built Distribution

spotipie-0.1.2-py2.py3-none-any.whl (35.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file spotipie-0.1.2.tar.gz.

File metadata

  • Download URL: spotipie-0.1.2.tar.gz
  • Upload date:
  • Size: 46.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.3

File hashes

Hashes for spotipie-0.1.2.tar.gz
Algorithm Hash digest
SHA256 837180679aefd04bc94edf8bcdcb154cbb5e8d94337eff2907dcb4d1716b4270
MD5 20a0ce464ac87d71d64868673b85f502
BLAKE2b-256 93f8a5e04b8fa1fea87fbd8a7cae2c6d160df0cfe1f248ecf94ffc8fae6174bf

See more details on using hashes here.

File details

Details for the file spotipie-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: spotipie-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 35.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.1 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.3

File hashes

Hashes for spotipie-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2538e108c404b6fcb768ab321d72f23deb28b10adf6df0f764be5160c78beba5
MD5 00609f571b849982e2e58a3a039a39c7
BLAKE2b-256 17a1d6e0084452a4e801c9cd1ac14388f7277c6e4eda91690b041bde3d350f07

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page