Spotify 客户端凭证流返回不受支持的 grant_type

问题描述 投票:0回答:1

我尝试使用凭据流从 Spotify API 检索 AuthToken (https://developer.spotify.com/documentation/general/guides/authorization/client-credentials/)。 尽管我遵循了所有步骤,但回应说

{
    "error": "unsupported_grant_type",
    "error_description": "grant_type parameter is missing"
}

This is the Request Body i used and the Response i got 如果你有兴趣,这是 Java 中的请求:

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 而不是错误。

rest spotify
1个回答
0
投票

您应该将其作为

Query Parameters
而不是
Request Body
传递。 我不确定如何在 Java 中的 HttpRequest 中传递查询参数,但是在 url 末尾添加参数就可以了。

.uri(URI.create("https://accounts.spotify.com/api/token/get?grant_type=client_credentials)

编辑:如本教程所述,我将 new URI 更改为 URI.createhttps://www.baeldung.com/java-httpclient-request-parameters

© www.soinside.com 2019 - 2024. All rights reserved.