【问题标题】:Trouble requesting Spotify access token [Bad Request]请求 Spotify 访问令牌时遇到问题 [错误请求]
【发布时间】:2017-07-20 03:05:01
【问题描述】:

我正在尝试使用Spotify developer page 上的“客户端凭据流”下定义的过程请求一个 spotify 访问令牌,

这是我当前的实现(当前返回 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 错误(不支持媒体)

【问题讨论】:

标签: c# asp.net asp.net-core spotify


【解决方案1】:

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

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

【讨论】:

  • 被单个字符卡住了 30 分钟,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多