英文:
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<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;
}
答案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(<bearer token from incoming auth header>).GetAsync();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论