出於某種原因,以下代碼給出了一個未經授權的異常,任何人都可以告訴我我做錯了什麼?辦公室365 - 多租戶應用程序不斷給未授權
在下面的截圖中你可以找到「theToken」,已經通過Office365接收並保存在數據庫中的內容(excecution前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
唯一的例外是:「類型'Microsoft.Office365.Discovery.DiscoveryFailedException'的異常被拋出。」 - 和錯誤代碼是「未授權」
根據下面的答案,我改變了下面的代碼:
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
但它不返回任何它在加載時的值。這就像api調用處於連續循環中一樣。
Identitical的問題,但更短:
我有一個Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache,我怎麼可以查詢使用DLL的和vb.net獲取數據的Microsoft日曆API。
更新我的問題,我更改了代碼,但它似乎沒有工作 – NicoJuicy
好吧,您可能想嘗試我們網站上的.NET入門教程並進行比較。我已經更新了我的答案。 –
我已經有了多個代碼示例,但它似乎沒有適用。 鑑於我有一個Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache(所以AccessToken和RefreshToken)保存在數據庫中。 如何在不調用DiscoveryClient的情況下查詢Outlook日曆API? – NicoJuicy