Consume Api rest with Visual Basic.net

0

beforehand thanks for any help, I would like to consume an api rest that has authentication OAuth2.0 ie by token.

I would like to begin is to be able to make the request post to the url sending the credentials but I have no idea how to do it and I have looked for examples on the web and I have to admit that they are very ambiguous.

If someone has an example or the structure of how to do it, he would be very grateful

    
asked by Jose Alejandro Alguinzones 28.05.2018 в 04:22
source

1 answer

0

You can use the RestSharp library, to install it use the following command in console package management

Install-Package RestSharp -Version 106.3.1

       using RestSharp;
       using RestSharp.Authenticators;
       using System.Collections;
       using System.Dynamic;

// With this method you get the token

        public string getToken(String sURL, String sUserName, String sPassword)
         {  String access_token = "";
             string clientId = "client";
             string clientSecret = "secret";
             string credentials = String.Format("{0}:{1}", clientId, clientSecret);
             RestClient restClient = new RestClient(sURL);
             RestRequest restRequest = new RestRequest("/oauth/token");
             restRequest.RequestFormat = DataFormat.Json;
             restRequest.Method = Method.POST;
             restRequest.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
             restRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
             restRequest.AddHeader("Accept", "application/json");
             restRequest.AddParameter("grant_type", "password");
             restRequest.AddParameter("username", sUserName);
             restRequest.AddParameter("password", sPassword);
             try {
                 var response = restClient.Execute(restRequest);
                 if (response.StatusCode == HttpStatusCode.BadRequest){
                     dynamic objError = new ExpandoObject();
                     objError = JsonConvert.DeserializeObject(response.Content);
                     access_token = "";
                     var error = objError.invalid_grant;
                     var error_description = objError.error_description;
                     MessageBox.Show(error + " - " + error_description, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
                 if (response.StatusCode == HttpStatusCode.Accepted || response.StatusCode == HttpStatusCode.OK){
                     dynamic objRpta = new ExpandoObject();
                     objRpta = JsonConvert.DeserializeObject(response.Content);
                     access_token = objRpta.access_token;
                     String token_type = objRpta.token_type;
                     int expires_in = objRpta.expires_in;
                 }
             }
             catch(IOException e){
                 MessageBox.Show(e.ToString(), "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
             return access_token;
        }
    
answered by 27.07.2018 в 01:14