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"}}))
关键修改说明:
- 移除手动Base64编码逻辑:
clj-http的:basic-auth接受[client-id client-secret]格式的向量,会自动生成正确的Authorization: Basic <编码后字符串>请求头,无需自行处理编码。 - 添加
form-params参数:Spotify的令牌接口要求通过表单形式传递grant_type=client_credentials,这是Client Credentials Flow的必填参数,缺失会直接触发400错误。
调用修正后的get-token函数即可正常获取令牌。
内容的提问来源于stack exchange,提问作者Jack Gee
相关产品推荐
相关产品推荐

