Search code examples
xmlhttprequestjson.netonenote

How to receive a response package from GET request for OneNote API


I'm getting a acknowledgement but no response message (details) i.e. list of notebooks from the OneNote API. Below is my code. I am able to receive the header and JSON details from a POST message but not the GET. I have tried to convert the POST code in order to submit a GET request.

private async void getRequestClick(object sender, RoutedEventArgs e)
    {
     await GetRequests(true, "test");
    }
    async public Task<StandardResponse> GetRequests(bool debug, string sectionName)
    {
         Uri PagesEndPoint1 = new Uri("https://www.onenote.com/api/v1.0/notebooks");
        var client = new HttpClient();
        //// Note: API only supports JSON return type.
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //// This allows you to see what happens when an unauthenticated call is made.
        if (IsAuthenticated)
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authClient.Session.AccessToken);
        }
        HttpResponseMessage response;
        HttpRequestMessage createMessage = new HttpRequestMessage(HttpMethod.Get, PagesEndPoint1);
        response = await client.SendAsync(createMessage);
        tbResponse.Text = response.ToString();
        return await TranslateResponse(response);
    }

    private async static Task<StandardResponse> TranslateResponse(HttpResponseMessage response)
    {
        StandardResponse standardResponse;
        if (response.StatusCode == HttpStatusCode.Created)
        {
            dynamic responseObject = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
            standardResponse = new CreateSuccessResponse
            {
                StatusCode = response.StatusCode,
                OneNoteClientUrl = responseObject.links.oneNoteClientUrl.href,
                OneNoteWebUrl = responseObject.links.oneNoteWebUrl.href
            };
        }
        else
        {
            standardResponse = new StandardErrorResponse
            {
                StatusCode = response.StatusCode,
                Message = await response.Content.ReadAsStringAsync()
            };
        }
        // Extract the correlation id.  Apps should log this if they want to collcet the data to diagnose failures with Microsoft support 
        IEnumerable<string> correlationValues;
        if (response.Headers.TryGetValues("X-CorrelationId", out correlationValues))
        {
            standardResponse.CorrelationId = correlationValues.FirstOrDefault();
        }
        return standardResponse;
    }

My POST messages are working OK. I can create a new page etc.


Solution

  • I think you need to change the expected status code from HttpStatusCode.Created to HttpStatusCode.OK for Get requests, since they return a 200 and not a 201. Try doing that in your TranslateResponse method.