请求 Spotify 访问令牌时遇到问题 [错误请求]

Trouble requesting Spotify access token [Bad Request]

我正在尝试使用 Spotify developer page

上的“Client Credentials Flow”下定义的过程来请求 Spotify 访问令牌

这是我当前的实现(目前 returns http 400 错误):

using (var client = new HttpClient())
{
    var requestBody = new StringContent(
        "grant_type:client_credentials", 
        Encoding.UTF8, 
        "application/x-www-form-urlencoded");

    var requestEncrypted = Convert.ToBase64String(
        Encoding.UTF8.GetBytes(ClientId + ":" + ClientSecret));

    client.DefaultRequestHeaders.Add(
         "Authorization", 
         $"Basic {requestEncrypted}");

    var tokenResult = await client.PostAsync(TokenEndpoint, requestBody); 
}

我尝试了其他格式化请求正文的方法,包括 json(注意:将编码类型更改为 'application/json' 会导致 http 415 错误(不支持媒体)

首先,您需要使用等号而不是冒号。

grant_type=client_credentials

这是一个example POST from the RFC

POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials