none
Invalid Token Or UnAuthorized Error....Unable to access Calendar Data after OAuth using Microsoft.Identity.Client Library RRS feed

  • Question

  • Hi,

    Background: We have taken office-365-business-premium account and we are experimenting to programmatically access Calendar Data.  For OAuth authentication we have used Microsoft.Identity.Client Library.

    Abstract of Code: 

    Following AppSettings are defined

       <add key="ClientId" value="xx4502ae-xxxx-4xx6-xxxx-761cxxxx4a5x" />
        <add key="BaseAddress" value="https://localhost:44321/" /> 

    In Code PublicClientApplication.AcquireTokenAsync() function does the job of asking credentials and authenticating user.

    result = await app.AcquireTokenAsync(new string[] { clientId  });
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.Token);
    tbLoginUser.Text = string.Format("{0}( {1} )", result.User.Name, result.User.DisplayableId);

    Next I am trying to get list of available calendars and passing received Token from oAuth result variable.

        Uri uri = new Uri("https://graph.microsoft.com/v2.0/me/calendars");
         HttpClient client = new HttpClient();
         client.DefaultRequestHeaders.Add("Authorization", "Bearer " + result.Token);
         HttpResponseMessage response = await client.GetAsync(uri);
         string retResp = await response.Content.ReadAsStringAsync();

    value in retResp reads as  and I don't understand why InvalidAuthenticationToken ?? 

    {
      "error": {
        "code": "InvalidAuthenticationToken",
        "message": "Access token validation failure.",
        "innerError": {
          "request-id": "5ee25c5d-9a47-4e89-a6f9-62dbc131d646",
          "date": "2017-03-21T13:25:38"
        }
      }
    }

    {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
      request-id: 5ee25c5d-9a47-4e89-a6f9-62dbc131d646
      client-request-id: 5ee25c5d-9a47-4e89-a6f9-62dbc131d646
      x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"South India","Slice":"SliceB","ScaleUnit":"002","Host":"AGSFE_IN_2","ADSiteName":"MAA"}}
      Date: Tue, 21 Mar 2017 13:25:37 GMT
      Content-Length: 244
      Content-Type: application/json; charset=utf-8
    }}

    Any Advice Please

    Regards

    Tuesday, March 21, 2017 1:30 PM

All replies

  • Hi,

    You also have taken the necessary steps to add the application to Azure Active Directory?


    Regards, Sjoukje

    Please remember to click 'Mark as Answer' on the post that helps you.

    Monday, March 27, 2017 6:49 AM
    Moderator