构建 API 调用 'Authorization' header MVC5
Building API Call 'Authorization' header MVC5
在我问这个问题之前,我想指出我已经阅读了 www.developer.intuit.com 上的 API 文档,并且我还在 Stack Overflow 上进行了搜索。我提到这个是因为我想避免任何包含 "why don't you read the Documentation or do a search" 的回复。据我所知,文档中实际上没有解决这个问题。
我正在尝试使用 HttpClient object 调用 API,我唯一缺少的是有关如何准确构建授权的详细信息 header。
这是构建和处理 API 调用的代码块:
string jsonString = JsonConvert.SerializeObject(customer, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var httpResponse = new HttpResponseMessage();
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppController.access_token);
httpResponse = await client.PostAsync(uri, httpContent);
}
我已进行了 Coder1409 建议的更改,并在上面的代码块中反映了该更改。我仍然收到错误,但错误编号已更改。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2017-12-04T07:57:11.131-08:00">
<Fault type="AuthenticationFault">
<Error code="100">
<Message>General Authentication Error</Message>
<Detail>AuthenticationErrorGeneral: Internal Server Error , statusCode: 500</Detail>
</Error>
</Fault>
</IntuitResponse>
认为你应该改变这一行
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth2", AppController.access_token);
到这个
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppController.access_token);
在我问这个问题之前,我想指出我已经阅读了 www.developer.intuit.com 上的 API 文档,并且我还在 Stack Overflow 上进行了搜索。我提到这个是因为我想避免任何包含 "why don't you read the Documentation or do a search" 的回复。据我所知,文档中实际上没有解决这个问题。
我正在尝试使用 HttpClient object 调用 API,我唯一缺少的是有关如何准确构建授权的详细信息 header。
这是构建和处理 API 调用的代码块:
string jsonString = JsonConvert.SerializeObject(customer, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var httpResponse = new HttpResponseMessage();
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppController.access_token);
httpResponse = await client.PostAsync(uri, httpContent);
}
我已进行了 Coder1409 建议的更改,并在上面的代码块中反映了该更改。我仍然收到错误,但错误编号已更改。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2017-12-04T07:57:11.131-08:00">
<Fault type="AuthenticationFault">
<Error code="100">
<Message>General Authentication Error</Message>
<Detail>AuthenticationErrorGeneral: Internal Server Error , statusCode: 500</Detail>
</Error>
</Fault>
</IntuitResponse>
认为你应该改变这一行
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth2", AppController.access_token);
到这个
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppController.access_token);