1

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

1 Answer 1

0

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.