我尝试使用凭据流从 Spotify API 检索 AuthToken (https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/)。 尽管我遵循了所有步骤,但回应说
{
"error": "unsupported_grant_type",
"error_description": "grant_type parameter is missing"
}
HttpRequest authRequest = HttpRequest.newBuilder()
.uri(new URI("https://accounts.spotify.com/api/token"))
.header("Authorization", "Basic " + Utilities.EncodeBase64(id + ":" + key))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(BodyPublishers.ofString(gson.toJson(new Auth("client_credentials"))))
.build();
我期待得到一个 AuthToken 而不是错误。
您应该将其作为
Query Parameters
而不是 Request Body
传递。
我不确定如何在 Java 中的 HttpRequest 中传递查询参数,但是在 url 末尾添加参数就可以了。
.uri(URI.create("https://accounts.spotify.com/api/token/get?grant_type=client_credentials)
编辑:如本教程所述,我将 new URI 更改为 URI.create。 https://www.baeldung.com/java-httpclient-request-parameters