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

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

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

问题

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

  1. public static async Task<DialogTurnResult> OnBehalfOf(WaterfallStepContext stepContext, CancellationToken cancellationToken)
  2. {
  3. try
  4. {
  5. IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
  6. .Create(clientId)
  7. .WithRedirectUri(redirectUri)
  8. .WithClientSecret(clientSecret)
  9. .Build();
  10. OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
  11. HttpProviderTeamsAccess temp = new HttpProviderTeamsAccess();
  12. GraphServiceClient graphClient = new GraphServiceClient(authProvider);
  13. var usr = await graphClient.Me.Request().GetAsync();
  14. var a = usr;
  15. //var reply_User_Name = MessageFactory.Text(user.DisplayName.ToString());
  16. //await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
  17. }
  18. catch (Exception ex)
  19. {
  20. var reply_User_Name = MessageFactory.Text(ex.Message.ToString());
  21. await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
  22. }
  23. return null;
  24. }
英文:

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

  1. public static async Task&lt;DialogTurnResult&gt; OnBehalfOf(WaterfallStepContext stepContext, CancellationToken cancellationToken)
  2. {
  3. try
  4. {
  5. IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
  6. .Create(clientId)
  7. .WithRedirectUri(redirectUri)
  8. .WithClientSecret(clientSecret)
  9. .Build();
  10. OnBehalfOfProvider authProvider = new OnBehalfOfProvider(confidentialClientApplication, scopes);
  11. HttpProviderTeamsAccess temp = new HttpProviderTeamsAccess();
  12. GraphServiceClient graphClient = new GraphServiceClient(authProvider);
  13. var usr = await graphClient.Me.Request().GetAsync();
  14. var a = usr;
  15. //var reply_User_Name = MessageFactory.Text(user.DisplayName.ToString());
  16. //await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
  17. }
  18. catch (Exception ex)
  19. {
  20. var reply_User_Name = MessageFactory.Text(ex.Message.ToString());
  21. await stepContext.Context.SendActivityAsync(reply_User_Name, cancellationToken);
  22. }
  23. return null;
  24. }

答案1

得分: 1

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

  1. 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.

  1. 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:

确定