如何在QuickBooks Online中创建日记条目

时间:2015-06-25 00:21:47

标签: c# asp.net .net quickbooks quickbooks-online

我正在尝试使用C#在Quickbooks Online V3 API中创建日记条目。

我做过的事情:

  • 检查JSON值和格式
  • Checked Header并接受。

标题信息:

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

我的代码:

   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()将创建标题,返回类型为字符串。

1 个答案:

答案 0 :(得分:0)

在您的网址中,我没有看到编码查询的使用,为什么要传递它?

请检查文档以获取CRUD和查询操作的正确端点 - https://developer.intuit.com/docs/api/accounting/JournalEntry

使用dev defined-

查看此示例以获取GET

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

同样在这里有一个帖子示例,你可以类似地写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;

        }
    }