ServiceStack BasicAuth 返回 401 client_credentials

ServiceStack BasicAuth returning 401 with client_credentials

我有一个远程端点需要基本身份验证和 grant_type 中的 client_credentials。

在 Postman 中我可以看到 headers 和 body 看起来像这样:

Request Headers:
  Content-Type: application/x-www-form-urlencoded
  Authorization: Basic <my Base64 encoded credentials>
  User-Agent: PostmanRuntime/7.15.2
  Accept: "*/*"
  Cache-Control: no-cache
  Postman-Token: <uuid>
  Host: <target url>
  Accept-Encoding: gzip, deflate
  Content-Length: 29
  Connection: keep-alive

Request Body:
  grant_type=client_credentials

如果我删除请求 body,端点 returns 401。

当我使用 ServiceStack HttpUtils 执行以下操作时:

var response = url.PostToUrl(
  formData: $"grant_type:\"client_credentials\", 
  requestFilter: req =>
  {
    req.AddBasicAuth(key, secret);
    req.ContentType = "application/x-www-form-urlencoded";
  });

我也收到 401 Unauthorized 异常,暗示 grant_type client_credentials 没有正确发布。在代码中解决这个问题需要什么?

我很快意识到正文需要是名称值对,所以这是复制邮递员发送的内容所需要的:

var response = url.PostToUrl(
  formData: new { grant_type = "client_credentials" }, 
  requestFilter: req =>
  {
    req.AddBasicAuth(key, secret);
    req.ContentType = "application/x-www-form-urlencoded";
  });