我正在调用graph.windows.net进行api调用,该调用以JSON形式返回并且它可以工作,但是,在一段随机的时间后,我的JSON数组返回为NULL并崩溃我的应用程序,可以在之后恢复我通过视觉工作室重新发布。
[Authorize]
public async Task<Dictionary<string,string>> UserApps()
{
string tenantId = ClaimsPrincipal.Current.FindFirst(TenantIdClaimType).Value;
// Get a token for calling the Windows Azure Active Directory Graph
AuthenticationContext authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, LoginUrl, tenantId));
ClientCredential credential = new ClientCredential(AppPrincipalId, AppKey);
AuthenticationResult assertionCredential = authContext.AcquireToken(GraphUrl, credential);
string authHeader = assertionCredential.CreateAuthorizationHeader();
string requestUrl = String.Format(
CultureInfo.InvariantCulture,
GraphApplicationsUrl,
HttpUtility.UrlEncode(tenantId),
HttpUtility.UrlEncode(User.Identity.Name));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
HttpResponseMessage response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
JObject responseObject = JObject.Parse(responseString);
JArray jsonArray = (JArray)responseObject["value"];
Dictionary<string, string> apps = new Dictionary<string, string>();
if (jsonArray != null && jsonArray.Count > 0)
{
foreach (JObject s in jsonArray)
{
if (!apps.ContainsKey(s["resourceDisplayName"].ToString()))
{
apps.Add(s["resourceDisplayName"].ToString(), s["resourceId"].ToString());
}
}
else
{
//Console.WriteLine(jsonArray.ToString());
}
return apps;
}
`
编辑:我认为@Filburt是正确的,它是关于API的请求限制。我会进一步研究为什么我达到这个限制。
答案 0 :(得分:0)
问题是我的身份验证令牌已过期。我只需要重新验证并解决了我的问题。