令牌无效时,MS Graph .netcore 客户端 SDK 抛出 NullReferenceException

MS Graph .netcore client SDK throws NullReferenceException when token invalid

我正在使用 Microsoft Graph .netcore SDK 版本 3.14 向 OneDrive For Business 发出请求。 但是,每当出现 401 Unauthorized 响应时,我都会收到 NullReferenceException 而不是正确的错误响应。 我应该如何处理这个错误,以便确定 NullReferenceException 的根本原因是 401? 以下是我请求删除 OneDrive 订阅的示例:

IGraphServiceSubscribedSkusCollectionRequestBuilder subscriptions = this.GetSubscriptions(credentials);
await subscriptions[webhookId]
   .Request()
   .DeleteAsync();

从 Fiddler 中,我可以看到响应是 401。 下面的答案是 NullReferenceException 的一个片段。它不包含任何表明由于 401 而失败的信息。

   Object reference not set to an instance of an object.
            
   at Microsoft.Graph.HttpProvider.<SendAsync>d__18.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__38.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.BaseRequest.<SendAsync>d__34`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.SubscriptionRequest.<DeleteAsync>d__4.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

这是由 SDK 中的错误引起的。请尝试升级到最新版本的sdk,看看问题是否仍然存在。

https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/issues/167