因此,我一直在使用Flask创建使用Spotify API的应用程序,并且在我的本地主机服务器上运行时,授权代码流工作得非常好。但是,将应用程序部署到Heroku之后,有时该应用程序将崩溃并在日志中给我一个“未提供令牌”错误。有时它会完美地工作,但是当我尝试使用它时,它会崩溃。我尝试在本地主机上重做该过程,但似乎没有中断。我将令牌存储在会话中,可能是Heroku在检索会话变量时遇到问题吗?
这是烧瓶应用程序代码
#Home view
@app.route('/')
@app.route('/home')
def home():
return render_template('home.html', title='Home')
#Login view
@app.route('/login', methods=['GET','POST'])
def login():
AUTH_FIRST = req_auth()
return redirect(AUTH_FIRST)
#Callback view for Spotify API
@app.route('/callback')
def callback():
if request.args.get('code'):
code = request.args.get('code')
token = req_token(code)
session['token'] = token
return redirect(url_for('generate_playlist'))
else:
return redirect(url_for('home'))
#Generate playlist view
@app.route('/generate_playlist', methods=['GET', 'POST'])
def generate_playlist():
if request.method == 'POST':
levels = int(float(request.form['level']))
token = session.get('token')
generate(token, levels)
return redirect(url_for('success'))
else:
return redirect(url_for('generate_playlist'))
这是后端授权代码(为了简化起见,我已经注释掉了一些部分,但仍然无法正常工作):
client_id = os.environ.get("CLIENT_ID")
client_secret = os.environ.get("CLIENT_SECRET")
redirect_uri = os.environ.get('REDIRECT_URI')
state = ''.join(random.choice(string.ascii_lowercase + string.digits) for n in range(8))
scope = 'user-top-read user-library-read playlist-modify-public'
def req_auth():
show_dialog = "false"
AUTH_FIRST_URL = f'https://accounts.spotify.com/authorize?client_id={client_id}&response_type=code&redirect_uri={quote("https://surprisify-playlist-generator.herokuapp.com/callback")}&show_dialog={show_dialog}&state={state}&scope={scope}'
return AUTH_FIRST_URL
def req_token(code):
#B64 encode variables
client_creds = f"{client_id}:{client_secret}"
client_creds_b64 = base64.b64encode(client_creds.encode())
#Token data
token_data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": "https://surprisify-playlist-generator.herokuapp.com/callback"
}
#Token header
token_header = {
"Authorization": f"Basic {client_creds_b64.decode()}"
}
#Make request post for token info
token_json = requests.post('https://accounts.spotify.com/api/token', data=token_data, headers=token_header)
return token_json.json()['access_token']
#Checking if token is still valid, otherwise, refresh
# if token_json.json()['expires_in']:
# now = datetime.datetime.now()
# expires_in = token_json.json()['expires_in']
# expires = now + datetime.timedelta(seconds=expires_in)
# if now > expires:
# refresh_token_data = {
# "grant_type": "refresh_token",
# "refresh_token": token_json.json()['refresh_token']
# }
# refresh_token_json = requests.post('https://accounts.spotify.com/api/token', data=refresh_token_data, headers=token_header)
# token = refresh_token_json.json()['access_token']
# return token
# else:
# token = token_json.json()['access_token']
# return token
# else:
# token = token_json.json()['access_token']
# return token
答案 0 :(得分:0)
重新启动浏览器后是否会发生错误? Flask会话Cookie会存储(默认情况下),直到您关闭浏览器。然后,如果您不再次进行身份验证,则调用ComboBoxes
将返回session.get(token)
,这很可能是您收到错误的原因。您可以尝试检查令牌在None
中是否为None
,并要求通过重定向进行重新认证。
这是我在自己的项目中成功使用的Spotipy实现的Spotify授权代码流:https://stackoverflow.com/a/57929497/6538328(方法2)。