mirror of
https://github.com/c0de-archive/spotipy.git
synced 2024-11-05 07:27:47 +00:00
Improved oauth docs
This commit is contained in:
parent
0c2fcecc16
commit
080786654a
@ -21,6 +21,17 @@ class SpotifyOAuth(object):
|
|||||||
|
|
||||||
def __init__(self, client_id, client_secret, redirect_uri,
|
def __init__(self, client_id, client_secret, redirect_uri,
|
||||||
state=None, scope=None, cache_path=None):
|
state=None, scope=None, cache_path=None):
|
||||||
|
'''
|
||||||
|
Creates a SpotifyOAuth object
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- client_id - the client id of your app
|
||||||
|
- client_secret - the client secret of your app
|
||||||
|
- redirect_uri - the redirect URI of your app
|
||||||
|
- state - security state
|
||||||
|
- scope - the desired scope of the request
|
||||||
|
- cache_path - path to location to save tokens
|
||||||
|
'''
|
||||||
self.client_id = client_id
|
self.client_id = client_id
|
||||||
self.client_secret = client_secret
|
self.client_secret = client_secret
|
||||||
self.redirect_uri = redirect_uri
|
self.redirect_uri = redirect_uri
|
||||||
@ -50,9 +61,14 @@ class SpotifyOAuth(object):
|
|||||||
|
|
||||||
def save_token_info(self, token_info):
|
def save_token_info(self, token_info):
|
||||||
if self.cache_path:
|
if self.cache_path:
|
||||||
|
try:
|
||||||
f = open(self.cache_path, 'w')
|
f = open(self.cache_path, 'w')
|
||||||
f.write(json.dumps(token_info))
|
f.write(json.dumps(token_info))
|
||||||
f.close()
|
f.close()
|
||||||
|
except IOError:
|
||||||
|
self._warn("couldn't write token cache to " + self.cache_path)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def is_token_expired(self, token_info):
|
def is_token_expired(self, token_info):
|
||||||
now = int(time.time())
|
now = int(time.time())
|
||||||
|
@ -3,21 +3,35 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
|
||||||
import oauth2
|
import oauth2
|
||||||
|
import spotipy
|
||||||
|
|
||||||
def prompt_for_user_token(username, scope=None):
|
def prompt_for_user_token(username, scope=None, client_id = None,
|
||||||
|
client_secret = None, redirect_uri = None):
|
||||||
''' prompts the user to login if necessary and returns
|
''' prompts the user to login if necessary and returns
|
||||||
the user token suitable for use with the spotipy.Spotify
|
the user token suitable for use with the spotipy.Spotify
|
||||||
constructor
|
constructor
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
- username - the Spotify username
|
||||||
|
- scope - the desired scope of the request
|
||||||
|
- client_id - the client id of your app
|
||||||
|
- client_secret - the client secret of your app
|
||||||
|
- redirect_uri - the redirect URI of your app
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
client_id = os.getenv('CLIENT_ID', 'YOUR_CLIENT_ID')
|
if not client_id:
|
||||||
client_secret = os.getenv('CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
|
client_id = os.getenv('CLIENT_ID')
|
||||||
redirect_uri = os.getenv('REDIRECT_URI', 'YOUR_REDIRECT_URI')
|
|
||||||
|
|
||||||
|
if not client_secret:
|
||||||
|
client_secret = os.getenv('CLIENT_SECRET')
|
||||||
|
|
||||||
if client_id == 'YOUR_CLIENT_ID':
|
if not redirect_uri:
|
||||||
|
redirect_uri = os.getenv('REDIRECT_URI')
|
||||||
|
|
||||||
|
if not client_id:
|
||||||
print '''
|
print '''
|
||||||
You need to set your Spotify API credentials. You can do this by
|
You need to set your Spotify API credentials. You can do this by
|
||||||
setting environment variables like so:
|
setting environment variables like so:
|
||||||
@ -29,10 +43,10 @@ def prompt_for_user_token(username, scope=None):
|
|||||||
Get your credentials at
|
Get your credentials at
|
||||||
https://developer.spotify.com/my-applications
|
https://developer.spotify.com/my-applications
|
||||||
'''
|
'''
|
||||||
sys.exit(1)
|
raise spotipy.SpotifyException(550, -1, 'no credentials set')
|
||||||
|
|
||||||
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
|
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
|
||||||
scope=scope, cache_path=username)
|
scope=scope, cache_path=".cache-" + username )
|
||||||
|
|
||||||
# try to get a valid token for this user, from the cache,
|
# try to get a valid token for this user, from the cache,
|
||||||
# if not in the cache, the create a new (this will send
|
# if not in the cache, the create a new (this will send
|
||||||
@ -60,6 +74,9 @@ def prompt_for_user_token(username, scope=None):
|
|||||||
print
|
print
|
||||||
print
|
print
|
||||||
response = raw_input("Enter the URL you were redirected to: ")
|
response = raw_input("Enter the URL you were redirected to: ")
|
||||||
|
print
|
||||||
|
print
|
||||||
|
|
||||||
code = sp_oauth.parse_response_code(response)
|
code = sp_oauth.parse_response_code(response)
|
||||||
token_info = sp_oauth.get_access_token(code)
|
token_info = sp_oauth.get_access_token(code)
|
||||||
# Auth'ed API request
|
# Auth'ed API request
|
||||||
|
Loading…
Reference in New Issue
Block a user