我需要为REST调用获取令牌。 令牌应位于JSON结果中。
https://github.com/bic-boxtech/BIC-BoxTech-API-Samples/wiki/Authentication
这是我尝试的方式:
datevalue
但是我得到400:
private async void GetBicDataAsync()
{
HttpClient _bicAothClient;
_bicAothClient = new HttpClient();
_bicAothClient.DefaultRequestHeaders.Add("Authorization", "Basic YmljYXBwOmJpY3NlY3JldGFwcA==");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Constants.BicAothUser),
new KeyValuePair<string, string>("password", Constants.BicAothPassword)
});
var result = await _bicAothClient.PostAsync(Constants.BicAothEndpoint, content);
}
我错了什么。我认为这是一个小小的愚蠢的失败。 有人可以帮我吗。
答案 0 :(得分:0)
我联系了提供商,发现... Wiki错误。我需要的两个URL都不正确。 现在,这里是获取所需内容的代码:
private async void GetBicDataAsync()
{
// HttpClient to get the token
HttpClient bicAothClient;
bicAothClient = new HttpClient();
// Add header
bicAothClient.DefaultRequestHeaders.Add("Authorization", "Basic YmljYXBwOmJpY3NlY3JldGFwcA==");
// Set parameters
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", <USERNAME>),
new KeyValuePair<string, string>("password", <PASSWORD>)
});
// POST async
var result = await _bicAothClient.PostAsync("https://app.bic-boxtech.org/oauth/token/", content);
// Get content as string
var contentString = await result.Content.ReadAsStringAsync();
// Parse content, to get the accessToken
var r = JToken.Parse(contentString);
var token = r["accessToken"].Value<string>();
// HttpClient to get container data
HttpClient _bicDataClient;
bicDataClient = new HttpClient();
// Add accessToken to header
bicDataClient.DefaultRequestHeaders.Add("Authorization", ("Bearer " + token));
// Get container data
var bicData = await bicDataClient.GetAsync("https://app.bic-boxtech.org/api/v2.0/container/" + <CONTAINERNUMBER>);
// Get content as string
var containerDataString = await bicData.Content.ReadAsStringAsync();
// Do what you need with this content
}
答案 1 :(得分:0)
如果您以字符串形式获取内容,则可以使用以下代码从端点中获取每个值:
// Ask if the container is found in dataBase
if (bicData.ReasonPhrase == "OK")
{
// Get content as string
var containerDataString = await bicData.Content.ReadAsStringAsync();
// Get a list of JObjects out of the content string
IEnumerable<JObject> objs = JsonConvert.DeserializeObject<List<JObject>>(containerDataString);
// Select the first JObject of the list
JObject containerDataJObject = objs.FirstOrDefault();
// Assign values as string
var tareKg = containerDataJObject["tare_kg"].Value<string>();
var tareLb = containerDataJObject["tare_lbs"].Value<string>();
var netKg = containerDataJObject["max_payload_kg"].Value<string>();
var netLb = containerDataJObject["max_payload_lbs"].Value<string>();
var maxKg = containerDataJObject["max_gross_mass_kg"].Value<string>();
var maxLb = containerDataJObject["max_gross_mass_lbs"].Value<string>();
var cubicM = containerDataJObject["cubic_capacity_cbm"].Value<string>();
var cubicFt = containerDataJObject["cubic_capacity_cuft"].Value<string>();
// And so on...
}
您将在Swagger example中获得对象的完整列表。