对象引用未设置为Microsoft Graph API调用的对象实例[重复]

时间:2022-10-22 11:25:08

This question already has an answer here:

这个问题在这里已有答案:

I'm calling graph.windows.net to make an api call which is returned as JSON and it works, however, after a random period of time my JSON array returns as NULL and crashes my application, which can be restored after I re-publish through visual studio.

我正在调用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;
    }
    `

EDIT: I believe that @Filburt is correct and that it's regarding the request limits to the API. I'll do some more research into why I'm hitting this limit.

编辑:我相信@Filburt是正确的,它是关于API的请求限制。我会进一步研究为什么我达到这个限制。

1 个解决方案

#1


0  

The issue is my authentication token is expiring. I just needed to re-authenticate and it solved my issue.

问题是我的身份验证令牌即将到期。我只需要重新验证并解决了我的问题。

#1


0  

The issue is my authentication token is expiring. I just needed to re-authenticate and it solved my issue.

问题是我的身份验证令牌即将到期。我只需要重新验证并解决了我的问题。