From 45232a24895912fc49a3b09b076f67401b82ccb2 Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Sat, 31 Dec 2016 09:33:09 -0500 Subject: [PATCH] improved example --- docs/index.rst | 39 ++++++++++++++++++++++++++++++- examples/user_public_playlists.py | 25 ++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 examples/user_public_playlists.py diff --git a/docs/index.rst b/docs/index.rst index 415b706..6c74f1f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -103,7 +103,19 @@ Register your app at `_. -*Spotipy* provides a +*spotipy* supports two authorization flows: + + - The **Authorization Code flow** This method is suitable for long-running applications + which the user logs into once. It provides an access token that can be refreshed. + + - The **Client Credentials flow** The method makes it possible + to authenticate your requests to the Spotify Web API and to obtain + a higher rate limit than you would + + +Authorization Code Flow +======================= +To support the **Authorization Code Flow** *Spotipy* provides a utility method ``util.prompt_for_user_token`` that will attempt to authorize the user. You can pass your app credentials directly into the method as arguments, or if you are reluctant to immortalize your app credentials in your source code, @@ -145,6 +157,31 @@ Here's an example of getting user authorization to read a user's saved tracks:: else: print "Can't get token for", username +Client Credentials Flow +======================= +To support the **Client Credentials Flow** *Spotipy* provides a +class SpotifyClientCredentials that can be used to authenticate requests like so:: + + + import spotipy + from spotipy.oauth2 import SpotifyClientCredentials + + client_credentials_manager = SpotifyClientCredentials() + sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) + + playlists = sp.user_playlists('spotify') + while playlists: + for i, playlist in enumerate(playlists['items']): + print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name'])) + if playlists['next']: + playlists = sp.next(playlists) + else: + playlists = None + +Client credentials flow is appropriate for requests that do not require access to a +user's private data. Even if you are only making calls that do not require +authorization, using this flow yields the benefit of a higher rate limit + IDs URIs and URLs ======================= *Spotipy* supports a number of different ID types: diff --git a/examples/user_public_playlists.py b/examples/user_public_playlists.py new file mode 100644 index 0000000..3557fc3 --- /dev/null +++ b/examples/user_public_playlists.py @@ -0,0 +1,25 @@ +# Gets all the public playlists for the given +# user. Uses Client Credentials flow +# + +import sys +import spotipy +from spotipy.oauth2 import SpotifyClientCredentials + +client_credentials_manager = SpotifyClientCredentials() +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) + +user = 'spotify' + +if len(sys.argv) > 1: + user = sys.argv[1] + +playlists = sp.user_playlists(user) + +while playlists: + for i, playlist in enumerate(playlists['items']): + print("%4d %s %s" % (i + 1 + playlists['offset'], playlist['uri'], playlist['name'])) + if playlists['next']: + playlists = sp.next(playlists) + else: + playlists = None