简体   繁体   English

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

[英]How to create Journal entry in QuickBooks Online

I am trying to use C# to create a Journal entry in Quickbooks Online V3 API . 我正在尝试使用C#在Quickbooks Online V3 API中创建日记条目。

Things I have done: 我所做的事情:

  • Check JSON Values and format 检查JSON值和格式
  • 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. GetDevDefineHeader()将创建标头,返回类型为字符串。

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 请检查文档以了解CRUD和查询操作的正确端点-https ://developer.intuit.com/docs/api/accounting/JournalEntry

See this example for GET using dev defined- 有关使用dev定义的GET的信息,请参见此示例,

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

Similarly there is a post example here, you can write similarly for JE- 同样,这里有一个帖子示例,您可以类似地为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;

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM