RestSharp.RestResponse而不是JSON字符串作为身份验证响应
我一直在努力使用REST API进行身份验证( https://api.vorexlogin.com/ ) 到目前为止,我已经添加了RestSharp库,以将错误和RestRequest的错误驳回。
到目前为止,当我在文本框中运行的代码时,我会得到的:
RestSharp.Response
需要的响应应该是:
"success": true,
"result": {
"accessToken": "eyJhbGtccccc.xxxx.yyyyzzzzz",
"refreshToken": "67ZYfdsJFDKLS879337937DKJSDLGFSDFRDEMO=",
"accessTokenExpireOn": "2021-07-03T22:19:23.8330686Z",
"refreshTokenExpireOn": "2021-08-02T21:49:23.8330686Z"
}
即使失败了,它仍然应该是某种类型的JSON文件。我将在下面提供我的代码。
private async void button1_Click(object sender, EventArgs e)
{
var client = new RestClient("https://api.vorexlogin.com/v2/security/authenticate");
var request = new RestRequest("resources", Method.Post);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("accept", "application/json");
request.AddParameter("application/x-www-form-urlencoded",
"grantType=password&userName=+"+ usernametbx + "&password=" + passwordtbx + "&tenant=" + tenanttbx, ParameterType.RequestBody);
var response = await client.PostAsync<IRestResponse>(request);
textBox1.Text = response.ToString();
}
public class IRestRepsonseList
{
public string accessToken { get; set; }
public string refreshToken { get; set; }
public string accessTokenExpireOn { get; set; }
public string refreshTokenExpireOn { get; set; }
}
public class IRestResponse
{
public bool success { get; set; }
public List<IRestRepsonseList> result { get; set; }
}
I have been working on using a Rest Api for authentication (https://api.vorexlogin.com/)
So far I have added the RestSharp library to dismiss errors to RestClient and RestRequest.
So far when I run the code that I have in the textbox I get this:
RestSharp.Response
The Response that is needed should be something like:
"success": true,
"result": {
"accessToken": "eyJhbGtccccc.xxxx.yyyyzzzzz",
"refreshToken": "67ZYfdsJFDKLS879337937DKJSDLGFSDFRDEMO=",
"accessTokenExpireOn": "2021-07-03T22:19:23.8330686Z",
"refreshTokenExpireOn": "2021-08-02T21:49:23.8330686Z"
}
Even if it failed it should still be a json file of some sort. I will provide my code below.
private async void button1_Click(object sender, EventArgs e)
{
var client = new RestClient("https://api.vorexlogin.com/v2/security/authenticate");
var request = new RestRequest("resources", Method.Post);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddHeader("accept", "application/json");
request.AddParameter("application/x-www-form-urlencoded",
"grantType=password&userName=+"+ usernametbx + "&password=" + passwordtbx + "&tenant=" + tenanttbx, ParameterType.RequestBody);
var response = await client.PostAsync<IRestResponse>(request);
textBox1.Text = response.ToString();
}
public class IRestRepsonseList
{
public string accessToken { get; set; }
public string refreshToken { get; set; }
public string accessTokenExpireOn { get; set; }
public string refreshTokenExpireOn { get; set; }
}
public class IRestResponse
{
public bool success { get; set; }
public List<IRestRepsonseList> result { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
postAsync&lt; t&gt;
,您告诉RestSharp对t
的响应进行挑战。您的iRestResponse
class不能实现toString()
,因此默认object.tostring
tostring 由您的代码使用,该代码仅返回实例类姓名。我还建议您阅读文档。
这是您应该做的示例:
By using
PostAsync<T>
you tell RestSharp to deserialize the response toT
. YourIRestResponse
class doesn't implementToString()
, so the defaultobject.ToString
is used by your code, which just returns the instance class name.I cloud also suggest you read the documentation, as you wrote lots of unnecessary code.
Here is an example of what you should be doing: