【问题标题】:Spotipy exception raised status 403; reason: noneSpotipy 异常引发状态 403;原因:无
【发布时间】:2021-07-19 16:20:39
【问题描述】:

我第一次尝试学习如何使用 API,然后我开始使用 spotipy。我正在关注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)

【问题讨论】:

    标签: python api flask spotipy


    【解决方案1】:

    更新:所以我从这个问题中休息了几个小时,然后回来调试它。我去了 Spotify 仪表板 - 删除了我的应用程序并创建了一个新应用程序。然后,我替换了我的客户端 ID 和客户端密码并再次运行该文件。它现在似乎工作了!不确定究竟是什么问题(可能是错误?),但创建一个新应用并替换您的凭据是可行的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多