API for office 365 from my web form

Hi,
I am trying to access our office 365 contact and mail from my web form through Azure:

Protected Async Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Using client = New HttpClient()
            Dim endpointUri As String, resourceId As String

            Using discoveryRequest = New HttpRequestMessage(HttpMethod.[Get], "https://api.office.com/discovery/v1.0/me/")
                discoveryRequest.Headers.Add("Authorization", "Bearer {token:https://api.office.com/discovery/}")

                Using discoveryResponse = Await client.SendAsync(discoveryRequest)
                    Dim discoverContent = Await discoveryResponse.Content.ReadAsStringAsync()
                    Dim serviceInfo = JObject.Parse(discoverContent)
                    endpointUri = "https://login.microsoftonline.com/db3f2b7e-a76f-40b3-bbbd-84c309b886e7/oauth2/authorize?api-version=1.0/"
                    resourceId = "https://api.office.com/discovery/"
                End Using
            End Using

            Using request = New HttpRequestMessage(HttpMethod.[Get], endpointUri & Convert.ToString("/me/folders/inbox/messages?$top=3"))
                request.Headers.Add("Authorization", (Convert.ToString("Bearer {token:") & resourceId) + "}")

                Using response = Await client.SendAsync(request)
                    Dim content = Await response.Content.ReadAsStringAsync()
                    For Each item In JArray.Parse(content)("value")
                    Console.WriteLine("Message ""{0}"" received at ""{1}""", Item("Subject"), Item("DateTimeReceived"))
                    Next
                End Using
            End Using
        End Using
    End Sub

But every time when I run the app I get this response error on:

For Each item In JArray.Parse(content)("value")  -> NullReference exception was handled by user code

and here is the error log:

 response    {StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:  {    x-ms-request-id: bec6dc24-eb37-4813-a5ce-5433acc73674    x-ms-gateway-service-instanceid: ESTSFE_IN_13    X-Content-Type-Options: nosniff    Strict-Transport-Security: max-age=31536000; includeSubDomains    Cache-Control: private    P3P: CP="DSP CUR OTPi IND OTRi ONL FIN"    Set-Cookie: flight-uxoptin=true; path=/; secure; HttpOnly    Set-Cookie: x-ms-gateway-slice=productiona; path=/; secure; HttpOnly    Set-Cookie: stsservicecookie=ests; path=/; secure; HttpOnly    Server: Microsoft-IIS/8.5    X-Powered-By: ASP.NET    Date: Wed, 03 Jun 2015 13:36:59 GMT    Content-Length: 7800    Content-Type: text/html; charset=utf-8  }}    System.Net.Http.HttpResponseMessage

and the contect show the Azure login to office 365:

am I typing my Authorization or token wrong?  I got the token from Azure manage site.

Do i need to get the token or end point uri from somewhere else?

Thanks for any advice.

Dean

June 3rd, 2015 9:43am

Yes, the token should be obtained via the OAuth2 Authorization Code Grant flow. I'd recommend that you take a look at the OAuth Sandbox, which does each step and shows you the request and response.
Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2015 10:27am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics