【发布时间】:2020-05-22 04:25:21
【问题描述】:
public static void CreateToken()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("grant_type", "client_credentials");
var UserPassJson = "{\"username\": \"mucode\",\"password\": \"mypassword\"}";
HttpContent content = new StringContent(UserPassJson, Encoding.UTF8, "application/json");
var response = client.PostAsync(new Uri("https://api.sandbox.paypal.com/v1/oauth2/token"), content).Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
string responseString = responseContent.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
}
}
为什么 response.IsSuccessStatusCode 显示状态码 401?什么原因导致故障? 什么操作会导致成功?
【问题讨论】:
-
缺少什么?
-
嗯,在文档中basic authentication 用于传递用户名和密码,正文是指定
grant_type=client_credentials的表单编码数据。相比之下,您添加了一个标头grant_type,并将用户名和密码作为 JSON 对象传递。
标签: c# asp.net api httpclient access-token