For some reason the following code gives an unauthorized exception, can anyone tell me what i'm doing wrong? 

In the screenshot below you can find the contents of "theToken", which has been received through Office365 and saved in a database ( 1 minute before excecution)

![Contents of theToken ( obfuscated)](https://i.stack.imgur.com/oImBX.png) 

```
 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

```

The exception is : "Exception of type ' Microsoft.Office365.Discovery.DiscoveryFailedException' was thrown." - and the ErrorCode is 'Unauthorized'

According to the answer below, i changed the following code:

```
  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()

```

Where my helper is : 

```
 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

```

But it doesn't return any value when it's loading.. It's like the api call is in a continuous loop.

Identitical question but shorter:

I have a Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache, how can i query the Microsoft Calendar API using the DLL's and vb.net to get the data.