Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# unsupported grant type when calling web api

I am trying to perform a Post to my WebAPI from a c# WPF desktop app.

No matter what I do, I get

{"error":"unsupported_grant_type"}

This is what I've tried (and I've tried everything I could find):

Also dev web api currently active for testing: http://studiodev.biz/

base http client object:

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("*/*")); 

with the following send methods:

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"); 

After that failed, I did some googling and tried:

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

same result, so I tried:

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(); })); }); 

Note: I can make a successful call with Chrome.

Update Fiddler Result

enter image description here

Could someone please help me make a successful call to the above web api... Please let me know if I can help clarify. Thanks!!

like image 623
OverMars Avatar asked Mar 25 '15 03:03

OverMars


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C called the mother of all languages?

C language is considered as the mother language of all the modern programming languages because most of the compilers, JVMs, Kernels, etc. are written in C language, and most of the programming languages follow C syntax, for example, C++, Java, C#, etc.


2 Answers

The default implementation of OAuthAuthorizationServerHandler only accepts form encoding (i.e. application/x-www-form-urlencoded) and not JSON encoding (application/JSON).

Your request's ContentType should be application/x-www-form-urlencoded and pass the data in the body as:

grant_type=password&username=Alice&password=password123 

i.e. not in JSON format.

The chrome example above works because it is not passing data as JSON. You only need this for getting a token; for other methods of your API you can use JSON.

This kind of problem is also discussed here.

like image 62
M. Ali Iftikhar Avatar answered Sep 17 '22 19:09

M. Ali Iftikhar


1) Note the URL: "localhost:55828/token" (not "localhost:55828/API/token")

2) Note the request data. Its not in json format, its just plain data without double quotes. "[email protected]&password=Test123$&grant_type=password"

3) Note the content type. Content-Type: 'application/x-www-form-urlencoded' (not Content-Type: 'application/json')

4) When you use javascript to make post request, you may use following:

$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) {//... 

See screenshots below from Postman:

Postman Request

Postman Request Header

like image 27
Chirag Avatar answered Sep 19 '22 19:09

Chirag