C#要请求oauth2令牌但获取错误:必须提供凭据
我使用此c#代码从网站API获取oauth2令牌。但它返回了这个 消息:状态代码400, “消息”:“必须提供凭据”, “标题”:“缺少所需参数” 有人知道问题出在哪里吗C#要请求oauth2令牌但获取错误:必须提供凭据,c#,api,http,oauth,token,C#,Api,Http,Oauth,Token,我使用此c#代码从网站API获取oauth2令牌。但它返回了这个 消息:状态代码400, “消息”:“必须提供凭据”, “标题”:“缺少所需参数” 有人知道问题出在哪里吗 using (var client = new HttpClient()) { var postData = new List<KeyValuePair<string, string>>();
using (var client = new HttpClient())
{
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("client_id", "abc"));
postData.Add(new KeyValuePair<string, string>("client_secret", "xxx"));
postData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
HttpContent content = new FormUrlEncodedContent(postData);
// content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var responseResult = client.PostAsync("https://sample.org/api/oauth/token", content).Result;
Label1.Content= responseResult.Content.ReadAsStringAsync().Result.ToString();
}
我想应该在错误中的某个地方有缺失参数的名称,您可能应该尝试一下这是Postman first。这更容易排除故障。看起来您缺少有关要获取令牌的用户的某些参数。我修改了question@ice7101but请提供完整的错误对象(无点)或者问题中的一个已经满了?我想你的帖子数据应该是一个键值对,而不仅仅是一个标准的HTTP请求字符串clientid=X&client\u secret=y&granttype=zI。我猜错误中应该有缺少的参数的名称。你可能应该先试试这是Postman。这更容易排除故障。看起来您缺少有关要获取令牌的用户的某些参数。我修改了question@ice7101but请提供完整的错误对象(无点)或者问题中的那一个已经满了?我怀疑你的帖子数据是否应该是一个键值对,而不仅仅是一个标准的HTTP请求字符串clientid=X&client\u secret=y&granttype=z
POST below request. Your new token can be found under ‘authentication_token’ in the response.
{
"client_id": "abc",
"client_secret": "123",
"grant_type": "client_credentials"
}