Rest 球拍-罐';t检索Spotify访问令牌:“;不支持的授予“U类型”;
我正在尝试获取Spotify API访问令牌。 根据Spotify API站点的说明: 请求将包括请求正文中的参数:Rest 球拍-罐';t检索Spotify访问令牌:“;不支持的授予“U类型”;,rest,curl,post,racket,spotify,Rest,Curl,Post,Racket,Spotify,我正在尝试获取Spotify API访问令牌。 根据Spotify API站点的说明: 请求将包括请求正文中的参数: 授予\u类型-设置为“客户端\u凭据” 此POST请求的标头必须包含以下参数: 授权-包含客户端ID和客户端密钥的Base 64编码字符串。该字段的格式必须为:Authorization:Basic 这通常通过向accounts.spotify.com发出POST请求来实现,如下所示: curl-X“POST”-H”授权:基本ZjM4ZjAw…WY0MzE=“-d gra
- 授予\u类型-设置为“客户端\u凭据”李>
- 授权-包含客户端ID和客户端密钥的Base 64编码字符串。该字段的格式必须为:
Authorization:Basic
curl-X“POST”-H”授权:基本ZjM4ZjAw…WY0MzE=“-d grant_type=client_凭证https://accounts.spotify.com/api/token
产量:{
“访问令牌”:“NgCXRKc…MzYjw”,
“令牌类型”:“承载者”,
“expires_in”:3600,
}
我试图在Racket中实现这一点,如下所示:
http.rkt
client.rkt
main.rkt
#朗球拍
(需要“client.rkt”)
(let([客户端(spotify客户端“”)]
[hc(hc)])
(gen访问令牌hc客户端)
但是我从SpotifyAPI得到的回应是
{“error”:“unsupported\u grant\u type”、“error\u description”:“grant\u type必须是客户端\u凭据、授权\u代码或刷新\u令牌”}
这很奇怪,因为它告诉我,即使我的授权类型是客户端凭据,也不支持我的授权类型(请参阅我在
上调用alist->form urlencoded
(列表(cons的授权类型“客户端凭据”)
)。是什么导致了这种情况?更新:我无法使用Racket的HTTP库让代码正常工作,但当我切换到使用简单HTTP包时,代码正常工作:
(define spotify
(update-headers
(update-ssl
(update-host json-requester "accounts.spotify.com") #t)
(list
"Content-Type: application/x-www-form-urlencoded"
"Authorization: Basic ...")))
(define response (json-response-body (post spotify "/api/token" #:data "grant_type=client_credentials")))
(displayln (hash-ref response 'access_token))
spotify是否有测试令牌可用于测试我的答案?或者我需要申请一个吗?(无论哪种方式都可以。:)@LeifAndersen我不相信有测试令牌-你必须从他们那里获得客户id和客户机密。
#lang racket
(require
net/base64 net/http-client net/url-connect
racket/bytes racket/port
"http.rkt")
(struct spotify-client (id secret))
(define (hc) (http-conn-open "accounts.spotify.com" #:ssl? #t))
(define (gen-access-token hc client)
(let* ([credentials (string-append (spotify-client-id client) ":" (spotify-client-secret client))]
[enc (bytes->string/utf-8 (base64-encode (string->bytes/utf-8 credentials)))]
[auth (string-append "Authorization: Basic " enc)]
[data (list (cons 'grant_type "client_credentials"))])
(post hc "/api/token" data auth)
(let-values ([(status-line header-list in-port) (http-conn-recv! hc)])
(displayln (port->string in-port)))))
(provide (all-defined-out))
#lang racket
(require "client.rkt")
(let ([client (spotify-client "<client-id>" "<client-secret>")]
[hc (hc)])
(gen-access-token hc client))
(define spotify
(update-headers
(update-ssl
(update-host json-requester "accounts.spotify.com") #t)
(list
"Content-Type: application/x-www-form-urlencoded"
"Authorization: Basic ...")))
(define response (json-response-body (post spotify "/api/token" #:data "grant_type=client_credentials")))
(displayln (hash-ref response 'access_token))