Object reference not set to an object while accessing Microsoft's Teams through .Net

huangapple go评论111阅读模式
英文:

Object reference not set to an object while accessing Microsoft's Teams through .Net

问题

在以下代码中,当控制尝试执行GetAsync()请求时,我遇到了空引用异常。

public static async Task<DialogTurnResult> OnBehalfOf(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    try
    {

        IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri(redirectUri)
        .WithClientSecret(clientSecret)
        .Build();

        OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
        HttpProviderTeamsAccess temp = new HttpProviderTeamsAccess();
        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        var usr = await graphClient.Me.Request().GetAsync();
        var a = usr;
        //var reply_User_Name = MessageFactory.Text(user.DisplayName.ToString());
        //await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
    }
    catch (Exception ex)
    {
        var reply_User_Name = MessageFactory.Text(ex.Message.ToString());
        await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
    }
    return null;
}
英文:

In the following code I'm getting a null reference exception when the control tries to execute the GetAsync() request.

public static async Task&lt;DialogTurnResult&gt; OnBehalfOf(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    try
    {

        IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
        .Create(clientId)
        .WithRedirectUri(redirectUri)
        .WithClientSecret(clientSecret)
        .Build();

        OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
        HttpProviderTeamsAccess temp = new HttpProviderTeamsAccess();
        GraphServiceClient graphClient = new GraphServiceClient(authProvider);

        var usr = await graphClient.Me.Request().GetAsync();
        var a = usr;
        //var reply_User_Name = MessageFactory.Text(user.DisplayName.ToString());
        //await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
    }
    catch (Exception ex)
    {
        var reply_User_Name = MessageFactory.Text(ex.Message.ToString());
        await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
    }
    return null;
}

答案1

得分: 1

使用OnBehalfOfProvider时,您必须指明您代表哪个用户进行调用。

var usr = await graphClient.Me.Request().WithUserAssertion(<来自传入授权头的Bearer令牌>).GetAsync();
英文:

When using the OnBehalfOfProvider you have to say which user you are making the call on behalf of.

var usr = await graphClient.Me.Request().WithUserAssertion(&lt;bearer token from incoming auth header&gt;).GetAsync();

huangapple
  • 本文由 发表于 2020年1月6日 21:03:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定