RestSharp.RestResponse而不是JSON字符串作为身份验证响应

发布于 2025-01-28 08:09:59 字数 1782 浏览 2 评论 0原文

我一直在努力使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2025-02-04 08:09:59

通过使用postAsync&lt; t&gt;,您告诉RestSharp对t的响应进行挑战。您的iRestResponse class不能实现toString(),因此默认object.tostring tostring 由您的代码使用,该代码仅返回实例类姓名。

我还建议您阅读文档。

这是您应该做的示例:

var client = new RestClient("https://api.vorexlogin.com/v2/security/authenticate");
var request = new RestRequest("resources", Method.Post)
    .AddParameter("grantType", "password")
    .AddParameter("userName", usernametbx)
    .AddParameter("password", passwordtbx)
    .AddParameter("tenant", tenanttbx);
var response = await client.PostAsync(request);
textBox1.Text = response.Content;

By using PostAsync<T> you tell RestSharp to deserialize the response to T. Your IRestResponse class doesn't implement ToString(), so the default object.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:

var client = new RestClient("https://api.vorexlogin.com/v2/security/authenticate");
var request = new RestRequest("resources", Method.Post)
    .AddParameter("grantType", "password")
    .AddParameter("userName", usernametbx)
    .AddParameter("password", passwordtbx)
    .AddParameter("tenant", tenanttbx);
var response = await client.PostAsync(request);
textBox1.Text = response.Content;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文