Python spotipy授权代码流

Python spotipy授权代码流,python,oauth-2.0,spotify,spotipy,Python,Oauth 2.0,Spotify,Spotipy,我正在使用Spotipy python库与Spotify web api交互。我已经阅读了API和文档,但没有看到一个清晰的示例显示库如何支持授权代码流() Spotipy库支持授权代码流,如文档所示。有关更多信息,您还可以查看Spotipy和。我在Spotipy的帮助下实现了一个简单的授权代码流。也许这对其他人也有帮助。同样在github上: 代码如下: from bottle import route, run, request import spotipy from spotipy imp

我正在使用Spotipy python库与Spotify web api交互。我已经阅读了API和文档,但没有看到一个清晰的示例显示库如何支持授权代码流()

Spotipy库支持授权代码流,如文档所示。有关更多信息,您还可以查看Spotipy和。

我在Spotipy的帮助下实现了一个简单的授权代码流。也许这对其他人也有帮助。同样在github上:

代码如下:

from bottle import route, run, request
import spotipy
from spotipy import oauth2

PORT_NUMBER = 8080
SPOTIPY_CLIENT_ID = 'your_client_id'
SPOTIPY_CLIENT_SECRET = 'your_client_secret'
SPOTIPY_REDIRECT_URI = 'http://localhost:8080'
SCOPE = 'user-library-read'
CACHE = '.spotipyoauthcache'

sp_oauth = oauth2.SpotifyOAuth( SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET,SPOTIPY_REDIRECT_URI,scope=SCOPE,cache_path=CACHE )

@route('/')
def index():

    access_token = ""

    token_info = sp_oauth.get_cached_token()

    if token_info:
        print "Found cached token!"
        access_token = token_info['access_token']
    else:
        url = request.url
        code = sp_oauth.parse_response_code(url)
        if code:
            print "Found Spotify auth code in Request URL! Trying to get valid access token..."
            token_info = sp_oauth.get_access_token(code)
            access_token = token_info['access_token']

    if access_token:
        print "Access token available! Trying to get user information..."
        sp = spotipy.Spotify(access_token)
        results = sp.current_user()
        return results

    else:
        return htmlForLoginButton()

def htmlForLoginButton():
    auth_url = getSPOauthURI()
    htmlLoginButton = "<a href='" + auth_url + "'>Login to Spotify</a>"
    return htmlLoginButton

def getSPOauthURI():
    auth_url = sp_oauth.get_authorize_url()
    return auth_url

run(host='', port=8080)
从瓶子导入路线、运行、请求
导入spotipy
从spotipy导入oauth2
端口号=8080
SPOTIPY_CLIENT_ID='您的客户_ID'
SPOTIPY_CLIENT_SECRET='您的客户_SECRET'
SPOTIPY_重定向_URI=http://localhost:8080'
作用域='用户库读取'
缓存='.spotipyoauthcache'
sp_oauth=oauth2.SpotifyOAuth(SPOTIPY_客户端ID、SPOTIPY_客户端机密、SPOTIPY_重定向_URI、scope=scope、cache_路径=cache)
@路由(“/”)
def index():
access_token=“”
token\u info=sp\u oauth.get\u cached\u token()
如果令牌信息:
打印“找到缓存令牌!”
访问令牌=令牌信息['access\u token']
其他:
url=request.url
code=sp_oauth.parse_response_代码(url)
如果代码:
打印“在请求URL中找到Spotify验证代码!正在尝试获取有效的访问令牌…”
令牌\u info=sp\u oauth.get\u access\u令牌(代码)
访问令牌=令牌信息['access\u token']
如果访问\u令牌:
打印“访问令牌可用!正在尝试获取用户信息…”
sp=spotipy.Spotify(访问令牌)
结果=sp.当前用户()
返回结果
其他:
返回htmlForLoginButton()
def htmlForLoginButton():
auth_url=getSPOauthURI()
HTMLOGINBUTTON=“”
返回HTMLOGIN按钮
def getSPOauthURI():
auth\u url=sp\u oauth.get\u authorize\u url()
返回身份验证url
运行(主机='',端口=8080)

当我尝试这样做时,不幸的是,这些答案都没有真正让我达到目的。当我最终弄明白这一点时,我在这篇文章中详细说明了:
我曾使用Django作为后端,但所有spotify api oauth的内容都是用javascript编写的,因此它对您仍然非常有用。

如果有人需要工作代码,这里是我的当前版本

请记住更改客户机id等。我将它们放在config.py中

import spotipy
import spotipy.util as util
from config import CLIENT_ID, CLIENT_SECRET, PLAY_LIST, USER
import random

token = util.oauth2.SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)

cache_token = token.get_access_token()
spotify = spotipy.Spotify(cache_token)

results1 = spotify.user_playlist_tracks(USER, PLAY_LIST, limit=100, offset=0)

嗨,谢谢你的例子。您知道如何刷新令牌吗?