使用 API 个 httpClient
consuming API httpClient
我在尝试使用 API 时遇到错误 "Bad Request"。我尝试了一些不同的方法,但没有成功。有人可以帮忙吗?
API 参数必须是:
FormData 参数
- 范围="oob"
- grant_type = "client_credentials"
Header 参数
- Content-type = "application/x-www-form-urlencoded"
- 授权 = "Basic 2xpZW50LTAxOnNlY3JldC1rZXktMDI="(Base64 示例)
[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();
}
您可以尝试以下代码片段
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();
}
我在尝试使用 API 时遇到错误 "Bad Request"。我尝试了一些不同的方法,但没有成功。有人可以帮忙吗?
API 参数必须是:
FormData 参数
- 范围="oob"
- grant_type = "client_credentials"
Header 参数
- Content-type = "application/x-www-form-urlencoded"
- 授权 = "Basic 2xpZW50LTAxOnNlY3JldC1rZXktMDI="(Base64 示例)
[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();
}
您可以尝试以下代码片段
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();
}