Office 365 - 多租户应用程序不断提供未经授权

Office 365 - multitenant application constantly gives unauthorized

出于某种原因,以下代码给出了未经授权的异常,谁能告诉我我做错了什么?

在下面的截图中可以看到"theToken"的内容,已经通过Office365接收并保存在数据库中(执行前1分钟)

 Async Function CalendarDemo() As System.Threading.Tasks.Task(Of ActionResult)

        Dim tokenData = UserTokenService.GetToken(HttpContext.User.Identity.Name)

        Dim theToken = tokenData.ReadItems(0)
        Dim myCalendars As List(Of ViewModels.Office365.MyCalendar) = New List(Of ViewModels.Office365.MyCalendar)

        Try

            Dim accessToken As Func(Of String) = Function() As String
                                                     Return theToken.AccessToken
                                                 End Function

            Dim discClient As New DiscoveryClient(New Uri("https://api.office.com/discovery/v1.0/me/"), accessToken)
            Dim dcr = Await discClient.DiscoverCapabilityAsync("Calendar") 'This is where the code breaks, the code worked in the past. Till i split up the authorization and the fetching of the Calendar



            ViewBag.ResourceId = dcr.ServiceResourceId
            Dim exClient As New OutlookServicesClient(dcr.ServiceEndpointUri, Function()
                                                                                  Return Helpers.Office365Helpers.ReturnStringAsAsync(theToken.AccessToken)
                                                                              End Function)
            Dim calendarResults = Await exClient.[Me].Events.ExecuteAsync()
            Dim calenderResults = Await exClient.Me.Calendar.Events.ExecuteAsync()

            Do
                Dim calendars = calendarResults.CurrentPage
                For Each calendar In calendars
                    Dim newCalendar = New ViewModels.Office365.MyCalendar(calendar)
                    myCalendars.Add(newCalendar)
                Next

                calendarResults = Await calendarResults.GetNextPageAsync()
            Loop While calendarResults IsNot Nothing
        Catch exception As AdalException
            ' handle token acquisition failure
            If exception.ErrorCode = AdalError.FailedToAcquireTokenSilently Then
                'authContext.TokenCache.Clear()

                'Redirect naar login of refresh token
                ViewBag.ErrorMessage = "AuthorizationRequired"
            End If
        Catch exception As DiscoveryFailedException

        End Try

        Return View(myCalendars)
    End Function

异常是:"Exception of type ' Microsoft.Office365.Discovery.DiscoveryFailedException' was thrown." - 错误代码是 'Unauthorized'

根据下面的回答,我修改了如下代码:

  Dim exClient As New OutlookServicesClient(New Uri("https://outlook.office365.com/api/v1.0/"), Function()
                                                                                                             Return Helpers.Office365Helpers.ReturnStringAsAsync(theToken.AccessToken)
                                                                                                          End Function)

Dim calendarResults = Await exClient.[Me].Events.Take(10).ExecuteAsync()

我的帮手在哪里:

 Public Module Office365Helpers
    Public Async Function ReturnStringAsAsync(ToReturnString As String) As System.Threading.Tasks.Task(Of String)


        Return Await System.Threading.Tasks.Task.Delay(1).ContinueWith(Function(dl) ToReturnString)

    End Function

End Module

但它在加载时 return 没有任何值。这就像 api 调用处于连续循环中。

相同的问题但更短:

我有一个 Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache,如何使用 DLL 和 vb.net 查询 Microsoft 日历 API 来获取数据。

您的资源应该是 https://api.office.com/discovery/ 用于调用发现服务。但是,由于您正在调用日历 API,您可以跳过发现。端点固定在 https://outlook.office365.com/api/v1.0/.

如果遇到困难,请尝试 .NET tutorial on https://dev.outlook.com

在 ODataException 的响应中,您可以查看 header 值。

那里有一个错误解释了我的错误,它说了一些内容:"Inconsistent resource"。这可能是真的。我发现在请求令牌时我没有使用 https://outlook.office365.com/api/v1.0/。但是在请求数据时使用它。

所以我收到了我尝试执行的无效令牌。