试图使用/token在Web上查询RestSharp。
在Fiddler中,我可以编写查询,它执行的方法没有问题: 1.将方法设置为username=username&password=pass&grant_type=password 2。添加一个标题:Content-Type: application/x-www-form-urlencoded 3。将正文设置为:Content-Type: application/x-www-form-urlencoded
试图在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"}知道为什么这不管用吗?另外,为什么在ContentType对象中有一个Paramter参数?我认为ContentType应该设置在标题中吗?(我还尝试从参数中删除ContentType )
谢谢
发布于 2017-04-22 19:44:21
看看这个有用不管用。
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);https://stackoverflow.com/questions/43547293
复制相似问题