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

Clojure调用Spotify凭证API遇400错误,求技术帮助

解决Spotify凭证API调用400错误的问题

你的代码存在两个关键问题,导致接口返回400错误:

  • 未传递Spotify令牌接口要求的必填表单参数grant_type=client_credentials
  • clj-http的:basic-auth参数用法错误:你手动编码了凭证,但该参数会自动处理Base64编码,重复编码会导致认证头无效

修正后的代码如下:

(ns api-test.spotify
  (:require [clj-http.client :as client]))

(def app-creds 
  {:id     "id creds here"
   :secret "secret creds here"})

(def token-url "https://accounts.spotify.com/api/token")

(defn get-token
  [app-creds]
  (client/post token-url
    {:basic-auth [(:id app-creds) (:secret app-creds)]
     :form-params {"grant_type" "client_credentials"}}))

关键修改说明:

  1. 移除手动Base64编码逻辑:clj-http的:basic-auth接受[client-id client-secret]格式的向量,会自动生成正确的Authorization: Basic <编码后字符串>请求头,无需自行处理编码。
  2. 添加form-params参数:Spotify的令牌接口要求通过表单形式传递grant_type=client_credentials,这是Client Credentials Flow的必填参数,缺失会直接触发400错误。

调用修正后的get-token函数即可正常获取令牌。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 23:33:22