简体   繁体   中英

consuming API httpClient

I am getting error "Bad Request" trying to consum an API. I had tryed some diferents ways, but without success. Could some one help?

API Parameters must be:

FormData Parameters

  1. scope = "oob"
  2. grant_type = "client_credentials"

Header Parameters

  1. Content-type = "application/x-www-form-urlencoded"
  2. Authorization = "Basic 2xpZW50LTAxOnNlY3JldC1rZXktMDI=" (Base64 example)

[POST]

curl -X POST \ https://api-sandbox.getnet.com.br/auth/oauth/v2/token \ -H 'authorization: Basic 2xpZW50LTAxOnNlY3JldC1rZXktMDI=' \ -H 'content-type: application/x-www-form-urlencoded' \ -d 'scope=oob&grant_type=client_credentials'

    string content_type = "application/x-www-form-urlencoded";
    string scope = "oob";
    string grant_type = "client_credentials";
    string authorization = "Basic 2xpZW50LTAxOnNlY3JldC1rZXktMDI="

    using (var httpClient = new HttpClient())
    {
         var requestMessage = new HttpRequestMessage()
         {
              Method = new HttpMethod("POST"),
              RequestUri = new Uri("https://api-sandbox.getnet.com.br/auth/oauth/v2/token"),
              Content = new StringContent(
                            @"{""scope"":""oob"",""grant_type"":client_credentials}", Encoding.UTF8, content_type)};

          requestMessage.Content.Headers.ContentType = 
                new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");

          requestMessage.Headers.Add("Authorization", authorization);

          var response = await httpClient.SendAsync(requestMessage);
          var responseStatusCode = response.StatusCode;
          var responseBody = await response.Content.ReadAsStringAsync();
    }

You can try following code snippets

  string content_type = "application/x-www-form-urlencoded";
  string scope = "oob";
  string grant_type = "client_credentials";
  string authorization = "Basic 2xpZW50LTAxOnNlY3JldC1rZXktMDI=";

  using (var httpClient = new HttpClient())
  {
    var parameters = new List<KeyValuePair<string, string>>() {
      new KeyValuePair<string, string>("scope", "oob"),
      new KeyValuePair<string, string>("grant_type", "client_credentials")
    };

    var requestMessage = new HttpRequestMessage()
    {
      Method = new HttpMethod("POST"),
      RequestUri = new Uri("https://api-sandbox.getnet.com.br/auth/oauth/v2/token"),
      Content = new FormUrlEncodedContent(parameters)
    };

    requestMessage.Content.Headers.ContentType =
          new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");

    requestMessage.Headers.Add("Authorization", authorization);

    var response = await httpClient.SendAsync(requestMessage);
    var responseStatusCode = response.StatusCode;
    var responseBody = await response.Content.ReadAsStringAsync();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM