0

I have been working on getting an api authentication to work and I am currently stuck on getting the response to execute to show up in a text box.

{
    private void button1_Click(object sender, EventArgs e)
    {
        string user = textBoxUsername.Text;
        string password = textBoxPassword.Text;
        string tenant = comboBoxCompany.Text;
        var client = new RestClient("https://server_name/v2/security/authenticate");
        var request = new RestRequest();
        request.Method = 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={{username}}&password={{password}}&tenant={{company_name}}", ParameterType.RequestBody);

        IRestResponse response = client.Execute(request); <= //error here // red line is under the 'Execute'

        textBoxJson.Text = response.ToString();


    }
}
public class IRestResponse
{
    public bool success { get; set; }
    public List<AuthenticationResponseData> result { get; set; }
}
public class AuthenticationResponseData
{
    public string accesstoken { get; set; }
    public string refreshtoken { get; set; }
    public string accessTokenExpiresOn { get; set; }
    public string refreshTokenExpiresOn { get; set; }        
    }

This is within a form page.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
Nic Bowles
  • 19
  • 1
  • 3
  • What is `RestClient`? Are you using [RestSharp](https://restsharp.dev/)? The error is pretty clear that `RestClient` doesn't have an `Execute` method. What led you to believe that it does? – Kirk Woll May 09 '22 at 16:18
  • Hello thanks for the response. I am new to using working with api authentication. I am using RestSharp. What would be the easiest way to write out method to execute the authentication within a class called RestClient? – Nic Bowles May 09 '22 at 16:21
  • I believe that both .Execute and the rest of the non-async methods have been deprecated in newer versions of RestSharp. There is a .ExecuteAsync method, however, `IRestResponse` is not available through RestSharp. Are you *sure* you're using RestSharp? – Lasse V. Karlsen May 09 '22 at 16:59
  • I suggest reading the [documentation](https://restsharp.dev/intro.html#basic-usage). I've never used RestSharp, but the example there shows `client.GetAsync()` being used... – Heretic Monkey May 09 '22 at 16:59

1 Answers1

0

var request = new RestSharp.RestRequest("RESOURCE", RestSharp.Method.POST) { RequestFormat = RestSharp.DataFormat.Json } .AddBody(BODY);

var response = Client.Execute(request);

// Handle response errors
HandleResponseErrors(response);

from How to POST request using RestSharp

JBatstone
  • 167
  • 7
  • 2
    And this is currently no longer working since .Execute has been deprecated. 10 year old answers should be validated to ensure they're still valid. – Lasse V. Karlsen May 09 '22 at 17:01