如何解决Spotipy 异常引发状态 403;理由:无
我第一次尝试学习如何使用 API 并且我使用了 spipy。我正在关注此任务的 tutorial here。我按照教程进行操作,直到他调用 current_user_saved_tracks(limit=50,offset=0)
并且一切正常。
但是,当我尝试将其切换到其他调用 current_user_top_tracks(limit=20,offset=0,time_range='medium_term')
时,它会引发错误:
spotipy.exceptions.SpotifyException: http status: 403,code:-1 - https://api.spotify.com/v1/me/top/tracks?time_range=medium_term&limit=20&offset=0:
error,reason: None
我指的是这些调用的 spotipy docs。 这是我到现在为止的代码。作为参考,调用是在第 40 行的 getTracks 函数中进行的:
import time
from flask import Flask,request,url_for,redirect,session
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from git_ignore.config import *
app = Flask(__name__)
TOKEN_INFO = "token_info"
@app.route('/')
def login():
sp_oauth = create_spotify_ouath()
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
@app.route('/redirect')
def redirectPage():
sp_oauth = create_spotify_ouath()
session.clear()
code = request.args.get('code')
token_info = sp_oauth.get_access_token(code)
session[TOKEN_INFO] = token_info
return redirect(url_for('getTracks',_external=True))
@app.route('/getTracks')
def getTracks():
try:
token_info = get_token()
except:
print("user not logged in")
return redirect('/')
sp = spotipy.Spotify(auth=token_info['access_token'])
return sp.current_user_top_tracks(time_range='medium_term',limit=20,offset=0)
# return sp.current_user_saved_tracks(limit=20,offset=0)
# The commented line above seems to work but the one with top_tracks throws an error.
def get_token():
token_info = session.get(TOKEN_INFO,None)
if not token_info:
raise "exception"
now = int(time.time())
is_expired = token_info['expires_at'] - now < 60
if (is_expired):
sp_oauth = create_spotify_ouath()
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
return token_info
def create_spotify_ouath():
return SpotifyOAuth(
client_id=client_id,client_secret=client_secret,# Where to come back to
redirect_uri=url_for('redirectPage',_external=True),scope="user-library-read")
if __name__ == "__main__":
app.run(debug=True)
解决方法
更新:所以我从这个问题中休息了几个小时,然后回来调试它。我去了 Spotify 仪表板 - 删除了我的应用程序并创建了一个新应用程序。然后,我替换了我的客户端 ID 和客户端密码并再次运行该文件。它现在似乎可以工作了!不确定问题究竟是什么(可能是错误?)但创建一个新应用并替换您的凭据有效!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。