You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地正常但PythonAnywhere中Spotipy配置失败问题求助

解决PythonAnywhere部署Spotipy OAuth重定向URL配置问题

我开发了一个需要持续运行的Spotify相关程序,选择部署在PythonAnywhere中。本地设备运行正常,但修改重定向URL后上传至PythonAnywhere却无法工作。确认问题出在配置环节——硬编码令牌时程序可正常运行至令牌过期。

程序核心代码如下:

def setup():
    spOauth  =  SpotifyOAuth(client_id=CLIENTID,
                             client_secret=CLIENTSECRET,
                             redirect_uri=REDIRECTURI,
                             scope=SCOPE,
                             show_dialog=True)
    return Spotify(auth_manager = spOauth)

def main():
    anon = "insert anon here"
    url = "insert supabase url here"
    supabase: Client = create_client(url,anon) #set up the connection to the database

    sp = setup() #set up the connection to spotipy
    refreshRate = 30

    current = {"length": 120000} #default used for if no song is being listened to
    run  = True
    count = -1

    while run:        
        nowp = getCurrentSong(sp)
        if nowp == 0:
            sp = setup()
        else:
            if nowp != current: 
                if count >=current["length"]/2000: #if youve listened to half the song add
                    SQLappend(current,supabase)
                if nowp!=-1: #if there is a new song
                    current = nowp #update current
                    refreshRate = current["length"]/4000 # and refreshrate
                else:
                    current = {"length": 120000} # if the music has stopped go back to default
                    refreshRate = current["length"]/4000
                count = 0 #reset timer
            elif nowp != -1: #if there is music
                count = count+refreshRate #keep track of for how long
            time.sleep(refreshRate)#wait some amount of time

排查与解决步骤

  • 精确匹配Spotify后台的重定向URI
    把PythonAnywhere用的重定向URL(比如https://<你的用户名>.pythonanywhere.com/callback)原封不动添加到Spotify开发者控制台的"重定向URI"列表中,必须保证协议、域名、路径完全一致,不能有任何拼写错误。本地开发用的URI可以保留,不影响部署。

  • 核对代码中的REDIRECTURI变量
    检查代码里的REDIRECTURI值是否和Spotify后台配置的完全一致,比如有没有漏写https、路径写错,或者大小写差异。PythonAnywhere环境建议使用HTTPS协议的URL,避免被Spotify的安全策略拦截。

  • 处理无头环境的授权缓存
    PythonAnywhere是无图形界面的环境,首次授权无法弹出浏览器完成验证。解决办法:

    1. 本地运行程序完成授权,把自动生成的.cache文件(Spotipy默认用来存储令牌的缓存文件)上传到PythonAnywhere的程序目录下;
    2. 或者在SpotifyOAuth中显式指定缓存路径,确保程序能读取到缓存的刷新令牌,实现自动续期。
  • 排查网络访问问题
    在PythonAnywhere的控制台中运行curl https://api.spotify.com/v1/me(带上有效令牌),测试是否能正常获取用户信息,排除服务器无法访问Spotify API的可能。

  • 添加日志定位具体错误
    修改setup()函数添加错误捕获,打印详细错误信息,方便排查:

    def setup():
        try:
            spOauth = SpotifyOAuth(client_id=CLIENTID,
                                 client_secret=CLIENTSECRET,
                                 redirect_uri=REDIRECTURI,
                                 scope=SCOPE,
                                 show_dialog=True)
            return Spotify(auth_manager=spOauth)
        except Exception as e:
            print(f"OAuth初始化失败: {str(e)}")
            return None
    

    然后查看PythonAnywhere的程序日志,就能知道是重定向不匹配、权限不足还是其他具体问题。

内容的提问来源于stack exchange,提问作者Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:12:22