NicoJuicy
NicoJuicy

Reputation: 3528

Office 365 - multitenant application constantly gives unauthorized

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)

 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.

Upvotes: 1

Views: 283

Answers (2)

NicoJuicy
NicoJuicy

Reputation: 3528

In the ODataException, in the response, you can view the header values.

There is an error there that explains my fault, it said something in the lines of : "Inconsistent resource". Which could be true. I found out that i didn't use https://outlook.office365.com/api/v1.0/ when requesting the token. But used it when requesting data.

So i received invalid tokens for what i tried to do.

Upvotes: 1

Jason Johnston
Jason Johnston

Reputation: 17702

Your resource should be https://api.office.com/discovery/ for calling the Discovery service. However, since you're calling the Calendar API, you could just skip discovery. The endpoint is fixed at https://outlook.office365.com/api/v1.0/.

If you get stuck, try the .NET tutorial on https://dev.outlook.com.

Upvotes: 0

Related Questions