RestSharp 不支持的授权类型
RestSharp unsupported grant type
正在尝试使用 RestSharp 在我的 Web API 上查询 /token
。
在 Fiddler 中,我可以编写查询并且通过以下方式执行没有问题:
1. 将方法设置为 POST
2.添加一个header:Content-Type: application/x-www-form-urlencoded
3、设置Body为:username=用户名&password=pass&grant_type=password
尝试在 RestSharp 中模仿:
RestClient tokenClient = new RestClient();
tokenClient.BaseUrl = new Uri(GlobalSettings.WebApiTokenUrl);
RestRequest req = new RestRequest(Method.POST);
req.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//req.Parameters.Add(new Parameter() { Name = "username", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType= "application/x-www-form-urlencoded" });
//req.Parameters.Add(new Parameter() { Name = "password", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
//req.Parameters.Add(new Parameter() { Name = "grant_type", Value = "password", Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
//req.Parameters.Add(new Parameter() { Name = "response_type", Value = "token", Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
req.AddParameter(new Parameter() { Name = "username", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
req.AddParameter(new Parameter() { Name = "password", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
req.AddParameter(new Parameter() { Name = "grant_type", Value = "password", Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
//req.AddParameter(new Parameter() { Name = "response_type", Value = "token", Type = ParameterType.RequestBody });
IRestResponse response = tokenClient.Execute(req);
var content = response.Content;
// I get: {"error":"unsupported_grant_type"}
知道为什么这行不通吗?
另外为什么Paramter
object里面有一个ContentType参数?我以为ContentType应该设置在header? (我也尝试从参数中删除 ContentType)
谢谢
看看这是否有效。
request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
string encodedBody = string.Format("username={0}&password= {1}&grant_type={2}", User.Identity.Name, User.Identity.Name,password);
request.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody);
正在尝试使用 RestSharp 在我的 Web API 上查询 /token
。
在 Fiddler 中,我可以编写查询并且通过以下方式执行没有问题:
1. 将方法设置为 POST
2.添加一个header:Content-Type: application/x-www-form-urlencoded
3、设置Body为:username=用户名&password=pass&grant_type=password
尝试在 RestSharp 中模仿:
RestClient tokenClient = new RestClient();
tokenClient.BaseUrl = new Uri(GlobalSettings.WebApiTokenUrl);
RestRequest req = new RestRequest(Method.POST);
req.AddHeader("Content-Type", "application/x-www-form-urlencoded");
//req.Parameters.Add(new Parameter() { Name = "username", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType= "application/x-www-form-urlencoded" });
//req.Parameters.Add(new Parameter() { Name = "password", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
//req.Parameters.Add(new Parameter() { Name = "grant_type", Value = "password", Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
//req.Parameters.Add(new Parameter() { Name = "response_type", Value = "token", Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
req.AddParameter(new Parameter() { Name = "username", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
req.AddParameter(new Parameter() { Name = "password", Value = User.Identity.Name, Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
req.AddParameter(new Parameter() { Name = "grant_type", Value = "password", Type = ParameterType.RequestBody, ContentType = "application/x-www-form-urlencoded" });
//req.AddParameter(new Parameter() { Name = "response_type", Value = "token", Type = ParameterType.RequestBody });
IRestResponse response = tokenClient.Execute(req);
var content = response.Content;
// I get: {"error":"unsupported_grant_type"}
知道为什么这行不通吗?
另外为什么Paramter
object里面有一个ContentType参数?我以为ContentType应该设置在header? (我也尝试从参数中删除 ContentType)
谢谢
看看这是否有效。
request.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
string encodedBody = string.Format("username={0}&password= {1}&grant_type={2}", User.Identity.Name, User.Identity.Name,password);
request.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody);