调用 Web 时 C# 不支持授权类型 api

C# unsupported grant type when calling web api

我正在尝试从 c# WPF 桌面应用程序对我的 WebAPI 执行 Post。

无论我做什么,我都会得到

{"error":"unsupported_grant_type"}

这是我尝试过的方法(我已经尝试了我能找到的一切):

还有开发网站 api 目前正在测试:http://studiodev.biz/

基础 http 客户端对象:

var client = new HttpClient()
client.BaseAddress = new Uri("http://studiodev.biz/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

使用以下发送方法:

var response = await client.PostAsJsonAsync("token", "{'grant_type'='password'&'username'='username'&'password'='password'");
var response = await client.PostAsJsonAsync("token", "grant_type=password&username=username&password=password");

失败后,我做了一些谷歌搜索并尝试:

LoginModel data = new LoginModel(username, password);
string json = JsonConvert.SerializeObject(data);
await client.PostAsync("token", new JsonContent(json));

相同的结果,所以我尝试了:

req.Content = new StringContent(json, Encoding.UTF8, "application/x-www-form-urlencoded");
await client.SendAsync(req).ContinueWith(respTask =>
{
 Application.Current.Dispatcher.Invoke(new Action(() => { label.Content = respTask.Result.ToString(); }));
});

注意:我可以用Chrome.

拨打成功

更新 Fiddler 结果

谁能帮我成功调用上面的网站api... 如果我可以帮助澄清,请告诉我。 谢谢!!

OAuthAuthorizationServerHandler 的默认实现只接受表单编码(即 application/x-www-form-urlencoded)而不接受 JSON 编码(application/JSON)。

您的请求的 ContentType 应该是 application/x-www-form-urlencoded 并且将正文中的数据传递为:

grant_type=password&username=Alice&password=password123

不是 JSON 格式

上面的 chrome 示例之所以有效,是因为它没有将数据作为 JSON 传递。你只需要这个来获得令牌;对于 API 的其他方法,您可以使用 JSON.

这种问题也有讨论here

这是一个工作示例,我曾使用 SSL 在端口 43305 上向我的本地 Web API 应用程序 运行 发出此请求。我也将项目放在 GitHub 上。 https://github.com/casmer/WebAPI-getauthtoken

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http;
using System.Web;

namespace GetAccessTokenSample
{
  class Program
  {
    private static string baseUrl = "https://localhost:44305";

    static void Main(string[] args)
    {

      Console.WriteLine("Enter Username: ");
      string username= Console.ReadLine();
      Console.WriteLine("Enter Password: ");
      string password = Console.ReadLine();

      LoginTokenResult accessToken = GetLoginToken(username,password);
      if (accessToken.AccessToken != null)
      {
        Console.WriteLine(accessToken);
      }
      else
      {
        Console.WriteLine("Error Occurred:{0}, {1}", accessToken.Error, accessToken.ErrorDescription);
      }

    }


    private static LoginTokenResult GetLoginToken(string username, string password)
    {

      HttpClient client = new HttpClient();
      client.BaseAddress = new Uri(baseUrl);
      //TokenRequestViewModel tokenRequest = new TokenRequestViewModel() { 
      //password=userInfo.Password, username=userInfo.UserName};
      HttpResponseMessage response =
        client.PostAsync("Token",
          new StringContent(string.Format("grant_type=password&username={0}&password={1}",
            HttpUtility.UrlEncode(username),
            HttpUtility.UrlEncode(password)), Encoding.UTF8,
            "application/x-www-form-urlencoded")).Result;

      string resultJSON = response.Content.ReadAsStringAsync().Result;
      LoginTokenResult result = JsonConvert.DeserializeObject<LoginTokenResult>(resultJSON);

      return result;
    }

    public class LoginTokenResult
    {
      public override string ToString()
      {
        return AccessToken;
      }

      [JsonProperty(PropertyName = "access_token")]
      public string AccessToken { get; set; }

      [JsonProperty(PropertyName = "error")]
      public string Error { get; set; }

      [JsonProperty(PropertyName = "error_description")]
      public string ErrorDescription { get; set; }

    }

  }
}

如果您使用的是 RestSharp,则需要这样请求:

public static U PostLogin<U>(string url, Authentication obj)
            where U : new()
        {
            RestClient client = new RestClient();
            client.BaseUrl = new Uri(host + url);
            var request = new RestRequest(Method.POST);
            string encodedBody = string.Format("grant_type=password&username={0}&password={1}",
                obj.username,obj.password);
            request.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody);
            request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
            var response = client.Execute<U>(request);

             return response.Data;

        }

1) 注意 URL:"localhost:55828/token"(不是 "localhost:55828/API/token")

2) 记下请求数据。它不是 json 格式,它只是没有双引号的纯数据。 "userName=xxx@gmail.com&password=Test123$&grant_type=password"

3) 注意内容类型。内容类型:'application/x-www-form-urlencoded'(不是内容类型:'application/json')

4) 当你使用javascript进行post请求时,你可以使用如下:

$http.post("localhost:55828/token", 
    "userName=" + encodeURIComponent(email) +
        "&password=" + encodeURIComponent(password) +
        "&grant_type=password",
    {headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}
).success(function (data) {//...

请参阅下面来自 Postman 的屏幕截图:

有同样的问题,但仅通过令牌 URL 的安全 HTTP 解决了我的问题。请参阅示例 httpclient 代码。服务器维护后普通HTTP停止工作

var apiUrl = "https://appdomain.com/token"
var client = new HttpClient();    
client.Timeout = new TimeSpan(1, 0, 0);
            var loginData = new Dictionary<string, string>
                {
                    {"UserName", model.UserName},
                    {"Password", model.Password},
                    {"grant_type", "password"}
                };
            var content = new FormUrlEncodedContent(loginData);
            var response = client.PostAsync(apiUrl, content).Result;

就我而言,我忘记了安装安装包 Token.JWT,所以您也需要在您的项目中安装。 Install-Package System.IdentityModel.Tokens.Jwt -版本 6.7.1

可能是协议的原因 它是必需的 https://

EX : https://localhost:port/oauth/token