简体   繁体   English

How to request Web API OAuth token using HttpClient in a C# WPF?

[英]How to request Web API OAuth token using HttpClient in a C# WPF?

I'm new in WPF and want to call oauth token api to get authenticated token through HttpClient.我是 WPF 的新手,想调用 oauth 令牌 api 以通过 HttpClient 获取经过身份验证的令牌。 I have tried many solutions but still getting response Bad request(400).我尝试了很多解决方案,但仍然收到错误请求(400)的响应。
I have checked with postman as well, api works fine.我也检查了 postman,api 工作正常。
postman response postman 响应

Tried Solutions:尝试过的解决方案:
(1) (1)

var request = new HttpRequestMessage(HttpMethod.Post, url);

request.Content = new FormUrlEncodedContent(param);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
request.Content.Headers.ContentType.CharSet = "UTF-8";

using (HttpResponseMessage response = await ApiHelper.apiClient.SendAsync(request))
{
    Console.WriteLine("postRequest -> StatusCode -> " + response.StatusCode);
    if (response.IsSuccessStatusCode)
    {
        //ComicModel comic = await response.Content.ReadAsAsync<ComicModel>();
        var responseString = await response.Content.ReadAsStringAsync();
        return response;
    }
    else
    {
        Console.WriteLine("postRequest -> exception -> " + response);
        throw new Exception(response.ReasonPhrase);
    }
}

(2) (2)

using (var content = new FormUrlEncodedContent(param))
{
    content.Headers.Clear();
    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    using (HttpResponseMessage response = await ApiHelper.apiClient.PostAsync(url, content))
    {
        Console.WriteLine("postRequest -> StatusCode -> " + response.StatusCode);
        if (response.IsSuccessStatusCode)
        {
            //ComicModel comic = await response.Content.ReadAsAsync<ComicModel>();
            var responseString = await response.Content.ReadAsStringAsync();
            return response;
        }
        else
        {
            Console.WriteLine("postRequest -> exception -> " + response);
            throw new Exception(response.ReasonPhrase);
        }
    }
}

(3) (3)

using (HttpResponseMessage response = await ApiHelper.apiClient.PostAsync(url, new FormUrlEncodedContent(param)))
{
    Console.WriteLine("postRequest -> StatusCode -> " + response.StatusCode);
    if (response.IsSuccessStatusCode)
    {
        //ComicModel comic = await response.Content.ReadAsAsync<ComicModel>();
        var responseString = await response.Content.ReadAsStringAsync();
        return response;
    }
    else
    {
        Console.WriteLine("postRequest -> exception -> " + response);
        throw new Exception(response.ReasonPhrase);
    }
}

All solutions respond me 400 bad request.所有解决方案都响应我 400 错误请求。
My params are:我的参数是:

var params= new List<KeyValuePair<string, string>>();
            params.Add(new KeyValuePair<string, string>("grant_type", "password"));
            params.Add(new KeyValuePair<string, string>("username", HttpUtility.UrlEncode("myUsername")));
            params.Add(new KeyValuePair<string, string>("password", HttpUtility.UrlEncode("myPass")));

How can I fix it?我该如何解决? Thank you in advance.先感谢您。

How do you authenticate with Postman?您如何使用 Postman 进行身份验证? I do not see it in your code.我在您的代码中没有看到它。 It depends on how the api is configured.这取决于 api 的配置方式。 Be sure to use the appropriate auth method in your client call, for example Bearer token.请务必在您的客户端调用中使用适当的身份验证方法,例如 Bearer 令牌。 Api should return 400 instead of 401. Api 应该返回 400 而不是 401。

httpClient.SetBearerToken(accessToken);

Oops.哎呀。 my base url starts with http.我的基础 url 以 http 开头。 I forgot to replace it with https.我忘了用 https 替换它。 Everything is fine with https: :) https 一切都很好::)
Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用HttpClient在C#中调用Web API? - How can I call a web api in c# using HttpClient? 如何在Web API中使用C#获取Foursquare OAuth请求代码? - How to get the Foursquare OAuth request code, using C# inside Web API? 在C#中为Vimeo API生成带有OAuth访问令牌的HTTP请求 - Generate HTTP request with OAuth access token for Vimeo API in c# 使用C#中的Oauth1从REST API获取请求令牌 - Obtain Request Token From REST API using Oauth1 in C# 如何使用 c# WPF 在 Web API 中发布带有子详细信息的数据? - How to Post data with Subdetails in Web API using c# WPF? 使用HttpClient()在C#控制台应用程序中对REST API的PUT请求 - PUT Request for REST API in C# console application using HttpClient() 在 C# 控制台应用程序中使用 HttpClient 使用 WEB API - Consuming WEB API using HttpClient in c# console application 如何使用来自 C# 的 OAuth-2.0 从 Etsy 请求刷新令牌 - How to request refresh token from Etsy using OAuth-2.0 from C# 流动客户端主题时如何在C#HttpClient中设置OIDC/OAuth承载令牌 - How to set OIDC/OAuth bearer token in C# HttpClient when flowing client subject 如何使用 C# HttpClient 向 Web API 提交文件和其他一些表单内容 - How to submit a file and some other form content to Web API using C# HttpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM