Microsoft Graph API调用无限期挂起 [英] Microsoft Graph API call hangs indefinitely

查看:64
本文介绍了Microsoft Graph API调用无限期挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Microsoft Graph查询用户信息.我可以很好地进行身份验证,但是当我尝试查询用户信息graphServiceClient.Users时,我的应用程序无限期挂起:没有超时,没有错误,只是挂起.我发现了这篇文章,但是那里的建议并没有帮助我.

I am attempting to query user information via Microsoft Graph. I can authenticate fine but when I attempt to query user information graphServiceClient.Users my application hangs indefinitely: no timeout, no error, just hangs. I found this post however the suggestions there did not help me.

这是我的代码:

public bool GetUserByUniqueID(string uid, out GraphUser user)
{
    bool ret = false;
    user = new GraphUser();
    if (Authenticate(out AuthToken token))
    {
        GraphServiceClient graphServiceClient = GetGraphServiceClient(token);
        // The below code hangs indefinitely
        User graphUser = graphServiceClient.Users[uid].Request()
            .Select("id,displayName,userPrincipalName,userType,accountEnabled").GetAsync().GetAwaiter()
            .GetResult();
        if (graphUser != null)
        {
            user.ID = graphUser.Id;
            user.DisplayName = graphUser.DisplayName;
            user.PrincipalName = graphUser.UserPrincipalName;
            user.UserType = graphUser.UserType;
            user.AccountEnabled = graphUser.AccountEnabled ?? false;
            user.MemberOf = GetMemberOf(token, graphUser.Id);
            ret = true;
        }
    }
    return ret;
}

private bool Authenticate(out AuthToken token)
{
    bool ret = false;
    token = new AuthToken();
    string url = $"https://login.microsoftonline.com/{_tenant}/oauth2/v2.0/token";
    RestClient client = new RestClient(url);
    RestRequest request = new RestRequest(Method.POST);
    request.Parameters.Add(new Parameter("grant_type", _grantType, ParameterType.GetOrPost));
    request.Parameters.Add(new Parameter("scope", _scope, ParameterType.GetOrPost));
    request.Parameters.Add(new Parameter("client_secret", _clientSecret, ParameterType.GetOrPost));
    request.Parameters.Add(new Parameter("client_id", _clientId, ParameterType.GetOrPost));
    IRestResponse response = client.Execute<AuthToken>(request);
    if (response.StatusCode == HttpStatusCode.OK)
    {
        token = JsonConvert.DeserializeObject<AuthToken>(response.Content);
        ret = true;
    }
    return ret;
}

推荐答案

我已通过恢复到以前版本的Microsoft.GraphMicrosoft.Graph.Core来解决此问题.我使用的是1.14.0版本,现在使用的是1.12.0. .GetAwaiter().GetResult()在当前版本(1.15.0)的1.14.0中被破坏.

I have fixed the issue by reverting to a previous version of Microsoft.Graph and Microsoft.Graph.Core. I was using version 1.14.0 and now I am using 1.12.0. .GetAwaiter().GetResult() is broken in 1.14.0 the current release (1.15.0).

这篇关于Microsoft Graph API调用无限期挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆