Search code examples
c#asp.net-mvcasp.net-mvc-5restful-authenticationintuit-partner-platform

Building API Call 'Authorization' header MVC5


Before I ask the question, I would like to point out that I have already been through the API Documentation at www.developer.intuit.com, and I have also done a search on Stack Overflow. I mention this because I want to avoid any responses that include "why don't you read the Documentation or do a search". As far as I can tell, this isn't actually addressed in the documentation.

I am attempting to call the API using an HttpClient object, and the only thing I am missing are the details concerning how exactly to construct the Authorization header.

This is the block of code that builds and handles the API call:

string jsonString = JsonConvert.SerializeObject(customer, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
var httpResponse = new HttpResponseMessage();
    using (var client = new HttpClient()) {
        client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
        client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppController.access_token);
        httpResponse = await client.PostAsync(uri, httpContent);
    }

I have made the change suggested by Coder1409, and reflected that change in the code block above. I am still getting an error, but the error number has changed.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2017-12-04T07:57:11.131-08:00">
    <Fault type="AuthenticationFault">
        <Error code="100">
        <Message>General Authentication Error</Message>
        <Detail>AuthenticationErrorGeneral: Internal Server Error , statusCode: 500</Detail>
        </Error>
    </Fault>
</IntuitResponse>

Solution

  • Think you should change this line

     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth2", AppController.access_token);
    

    to this

     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppController.access_token);