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

刷新Spotify Access Token时遭遇Error 400错误求助

刷新Spotify Access Token时遭遇400 Bad Request错误

我尝试使用refresh token刷新Spotify Access Token,已遵循Spotify官方文档操作,且尝试寻找解决方案但未能解决问题。

用于刷新操作的代码

def refresh_the_token():
    auth_client = client_id + ":" + client_secret
    auth_encode = 'Basic ' + base64.b64encode(auth_client.encode()).decode()

    headers = {
        'Authorization': auth_encode,
        'Content - Type': 'application/x-www-form-urlencoded'
    }

    data = {
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token
    }

    response = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)  # sends request off to spotify

    if response.status_code == 200:  # checks if request was valid
        print("The request to went through we got a status 200; Spotify token refreshed")
        response_json = response.json()
        new_expire = response_json['expires_in']
        print("the time left on new token is: " + str(new_expire / 60) + "min")  # says how long
        return response_json["access_token"]
    else:
        print("ERROR! The response we got was: " + str(response))

遇到的错误

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>400 Bad Request</title>

</head>

<body text=#000000 bgcolor=#ffffff>

<h1>Error: Bad Request</h1>
<h2>Your client has issued a malformed or illegal request.</h2>
<h2></h2>

</body>

</html>

Spotify官方文档(请求刷新访问令牌)核心要求

  • 使用POST请求访问https://accounts.spotify.com/api/token
  • 请求头必须包含:
    • Authorization: Basic <base64编码的client_id:client_secret>
    • Content-Type: application/x-www-form-urlencoded
  • 请求体必须携带两个参数:
    • grant_type=refresh_token
    • refresh_token=<你的有效刷新令牌>

问题排查与修复方案

  1. 修复请求头的Content-Type字段
    代码中'Content - Type'存在多余空格,这会导致Spotify无法正确识别请求格式,是引发400错误的直接原因。正确的字段名应为'Content-Type'。

  2. 确保base64编码无异常
    拼接client_id和client_secret时避免多余空格,编码后记得去除可能的换行或空白字符。

  3. 验证refresh_token有效性
    确认refresh_token是首次授权流程中正确获取的,未被撤销或过期。

修复后的代码示例

def refresh_the_token():
    auth_client = f"{client_id}:{client_secret}"
    # 规范编码流程,避免异常字符
    auth_encode = 'Basic ' + base64.b64encode(auth_client.encode('utf-8')).decode('utf-8').strip()

    headers = {
        'Authorization': auth_encode,
        'Content-Type': 'application/x-www-form-urlencoded'  # 移除空格,修正字段名
    }

    data = {
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token
    }

    response = requests.post('https://accounts.spotify.com/api/token', data=data, headers=headers)

    if response.status_code == 200:
        print("请求成功,状态码200;Spotify令牌已刷新")
        response_json = response.json()
        new_expire = response_json['expires_in']
        print(f"新令牌剩余有效期:{new_expire / 60:.2f}分钟")
        return response_json["access_token"]
    else:
        # 输出更详细的错误信息,便于进一步排查
        print(f"错误!响应状态码:{response.status_code}")
        print(f"错误响应内容:{response.text}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 20:22:35