该prompt_for_user_token
函数是 Spotipy 提供的某些功能的简写。这是函数本身的代码。
https://github.com/plamere/spotipy/blob/master/spotipy/util.py
[ -- snip -- ]
def prompt_for_user_token(username, scope=None, client_id = None,
client_secret = None, redirect_uri = None, cache_path = None):
''' prompts the user to login if necessary and returns
the user token suitable for use with the spotipy.Spotify
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
- cache_path - path to location to save tokens
'''
[ -- snip -- ]
cache_path = cache_path or ".cache-" + username
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri,
scope=scope, cache_path=cache_path)
[ -- snip -- ]
token_info = sp_oauth.get_cached_token()
if not token_info:
print('''
User authentication requires interaction with your
web browser. Once you enter your credentials and
give authorization, you will be redirected to
a url. Paste that url you were directed to to
complete the authorization.
''')
auth_url = sp_oauth.get_authorize_url()
try:
import webbrowser
webbrowser.open(auth_url)
print("Opened %s in your browser" % auth_url)
except:
print("Please navigate here: %s" % auth_url)
[ -- snip -- ]
您可以复制代码并使用auth_url
(例如)popen
模块和,作为参数chrome.exe
传递。auth_url
提示:在 windows 下cmd.exe /c start http://your.website/address
会将地址转发到您当前的默认浏览器。(相对于硬编码firefox.exe
或chrome.exe
)