How to create Journal entry in QuickBooks Online

2.2k views Asked by At

I am trying to use C# to create a Journal entry in Quickbooks Online V3 API.

Things I have done:

  • Check JSON Values and format
  • Checked Header and accept.

Header info:

Accept : application/json
Authorization : OAuth oauth_token="************",oauth_nonce="cfc36792-8f9a-4202-9fec-be0a610eed8e",oauth_consumer_key="************",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1435189421",oauth_version="1.0",oauth_signature="***"
Content-Length : 642
Content-Type : application/json
Host : qb.sbfinance.intuit.com

//till here everything is fine not sure about last two lines..
//not sure should i add this to my header or not..

X-NewRelic-ID : UQMAU15RGwcAUllSDwc=
X-NewRelic-Transaction : PxQAVVRUCgMIUiRXdHMBICETGlUDChAHHEAPVgoPBgILU3xUciMFJCEUG0MDUwFVAV1WVBVs

My code:

   public static void Main()
        {

         //   JournalEndPoint lnew = new JournalEndPoint();
           // string Json=lnew.CreateJournal();

            string consumerKey = "***";
            string consec = "***";
            string appkeysec1 = "***";
            string appkey1 = "***";

            string returnvalue = ExecuteV3Query(consumerKey, consec, appkey1, appkeysec1, "1397754725", "select * from employee where active=TRUE");

        }
        public static string ExecuteV3Query(string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret, string CompanyId, string query)
        {
            string encodedQuery = RestSharp.Contrib.HttpUtility.UrlEncode(query);
            string uri = string.Format("https://quickbooks.api.intuit.com/v3/company/{0}/journalentry", CompanyId, encodedQuery);
            HttpWebRequest httpWebRequest = WebRequest.Create(uri) as HttpWebRequest;
            httpWebRequest.Method = "POST";
            string JSonvalue = CreateUserJson();
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Accept = "application/json";
            httpWebRequest.Headers.Add("Authorization", GetDevDefinedOAuthHeader(consumerKey, consumerSecret, accessToken, accessTokenSecret, httpWebRequest, null));
            //HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

            /*
             * using (Stream data = httpWebResponse.GetResponseStream())
            {
                //return XML response
                return new StreamReader(data).ReadToEnd();
            }
            */


            var result = "";
            HttpWebResponse httpResponse;
            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                    using (Stream data = e.Response.GetResponseStream())
                    using (var reader = new StreamReader(data))
                    {
                        string text = reader.ReadToEnd();
                        Console.WriteLine(text);
                        result = text;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return result;
        }

GetDevDefineHeader() will create header and return type is string.

1

There are 1 answers

9
nimisha shrivastava On

In your url, I do not see the use of encoded query and why are you passing it?

Please check the docs for correct endpoints for CRUD and query operations- https://developer.intuit.com/docs/api/accounting/JournalEntry

See this example for GET using dev defined-

https://gist.github.com/IntuitDeveloperRelations/0913b4c224de758fde0a

Similarly there is a post example here, you can write similarly for JE-

//string res = CreateV3Customer(consumerKey, consumerSecret, accessToken, accessTokenSecret, realmId);

public string CreateV3Customer(string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret, string realmId)
    {

        StringBuilder request = new StringBuilder();
        StringBuilder response = new StringBuilder();



        var requestBody = "{\"FamilyName\":\"Jack\"}";

        HttpWebRequest httpWebRequest = WebRequest.Create("https://quickbooks.api.intuit.com/v3/company/"+realmId+"/customer") as HttpWebRequest;
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Headers.Add("Authorization", GetDevDefinedOAuthHeader(consumerKey, consumerSecret, accessToken,accessTokenSecret,httpWebRequest, requestBody));
        request.Append(requestBody);
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] content = encoding.GetBytes(request.ToString());
        using (var stream = httpWebRequest.GetRequestStream())
        {
            stream.Write(content, 0, content.Length);
        }
        HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
        using (Stream data = httpWebResponse.GetResponseStream())
        {


          string customerr = new StreamReader(data).ReadToEnd();

          return customerr;

        }
    }