Reputation: 131
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
Header Parameters
[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();
}
Upvotes: 0
Views: 492
Reputation: 346
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();
}
Upvotes: 3