簡體   English   中英

創建付款發票時,Sage One API返回500內部服務錯誤

[英]Sage One API returning 500 Internal Service Error when creating Payment Invoice

在Sage One API中創建“購買發票”時遇到問題。 無論我對傳入的數據進行什么更改,我似乎都會收到500內部服務錯誤,並且沒有包含任何有意義信息的詳細響應。 無論輸入什么數據,響應始終是相同的(在errorCode字段中只有一個GUID,並且服務器時間在變化)。 響應如下:

{
"$diagnoses": [
{
  "$severity": "error",
  "$dataCode": "UnexpectedError",
  "$message": "Unexpected error",
  "$source": {
    "serverTime": "2016-04-29T15:48:11.637+00:00",
    "errorCode": "3b83e1b5-164d-42d4-95b3-3479b09c93f1",
    "requestPath": "/api/v2/purchase_invoices",
    "queryString": "",
        "requestMethod": "POST"
      }
     }
  ]
}

我很肯定這不是授權問題,因為在此之前我都會獲取並創建其他數據類型。 這包括創建采購發票所需的聯系人。

我一直在他們自助網站上提供的信息中進行跟蹤。 我還從他們的裸露SDK開始,這是我正在使用的大多數基礎流程的基礎。 從那以后,我重寫了大多數底層結構,最初認為它可能是由SDK引起的,這導致我遇到了同樣的情況-我只收到500個內部服務錯誤作為響應。

這使我相信這是參數本身的問題,因為文檔在中間一列中列出的參數與右一列中的示例調用中的數據之間存在一些差異。 具體而言,該示例在中間列中未列出的額外字段(例如“ extra_reference”)以及“ product_id”和“ product_code”行項目中。

這是該調用的一些相關代碼,再次記住基本體系結構是它們的基本結構,並進行了一些程序修改以適合我當前的體系結構,而這些修改不會影響實際的調用:

    protected override List<KeyValuePair<string, string>> GetPostDataValuesForCreate(
        SageOnePurchaseInvoice objectToCreate)
    {
        List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>();

        postData.Add(new KeyValuePair<string, string>("expense[contact_id]", objectToCreate.ContactId.ToString()));
        postData.Add(new KeyValuePair<string, string>("expense[date]", objectToCreate.Date));
        postData.Add(new KeyValuePair<string, string>("expense[due_date]", objectToCreate.DueDate));

        if (!string.IsNullOrWhiteSpace(objectToCreate.Reference))
        {
            postData.Add(new KeyValuePair<string, string>("expense[reference]", objectToCreate.Reference));
        }

        if (objectToCreate.ProjectId != 0)
        {
            postData.Add(new KeyValuePair<string, string>("expense[project_id]", objectToCreate.ProjectId.ToString()));
        }

        if (!string.IsNullOrWhiteSpace(objectToCreate.Notes))
        {
            postData.Add(new KeyValuePair<string, string>("expense[notes]", objectToCreate.Notes));
        }


        for (int i = 0; i < objectToCreate.LineItems.Length; i++)
        {
            string index = "expense[line_items_attributes][" + i.ToString() + "][";
            postData.Add(new KeyValuePair<string, string>(index + "description]", objectToCreate.LineItems[i].Description));
            postData.Add(new KeyValuePair<string, string>(index + "ledger_account_id]", objectToCreate.LineItems[i].LedgerAccount.Id.ToString()));
            postData.Add(new KeyValuePair<string, string>(index + "quantity]", objectToCreate.LineItems[i].Quantity.ToString()));
            postData.Add(new KeyValuePair<string, string>(index + "unit_price]", objectToCreate.LineItems[i].UnitPrice.ToString()));
        }

        return postData;
    }

這是創建和執行Web請求的實際方法:

公共靜態字符串Create(Uri baseUrl,List> bodyParams,字符串令牌,字符串signingSecret){字符串結果;

        string nonce = GenerateNonce();
        HttpWebRequest sageOneWebRequest = WebRequest.Create(baseUrl) as HttpWebRequest;

        string PostParams = ConvertPostParams(bodyParams);

        string signatureString = GetSignatureString(baseUrl, null, bodyParams, nonce, WebRequestMethods.Http.Post);
        string signingKey = GetSigningKey(token, signingSecret);
        string signature = GenerateHmac(signingKey, signatureString);

        sageOneWebRequest.AllowAutoRedirect = true;
        sageOneWebRequest.Accept = "*/*";
        sageOneWebRequest.UserAgent = "Itemize";
        sageOneWebRequest.Headers.Add("X-Signature", signature);
        sageOneWebRequest.Headers.Add("X-Nonce", nonce);
        sageOneWebRequest.ContentType = "application/x-www-form-urlencoded";
        sageOneWebRequest.Timeout = 100000;
        sageOneWebRequest.Headers.Add("Authorization", "Bearer " + token);
        sageOneWebRequest.Method = WebRequestMethods.Http.Post;

        using (StreamWriter requestWriter = new StreamWriter(sageOneWebRequest.GetRequestStream()))
        {
            try
            {
                requestWriter.Write(PostParams);
            }
            catch(Exception ex)
            {
                throw new Exception("Exception thrown writing Post Params to Body", ex);
            }
        }

        try
        {
            using (WebResponse response = sageOneWebRequest.GetResponse())
            {
                Stream dataStream = response.GetResponseStream();
                using (StreamReader reader = new StreamReader(dataStream))
                {
                    result = reader.ReadToEnd();
                }
            }
        }
        catch (WebException webex)
        {
            //This is where the error is caught
            Logger.Error("Web Exception while processing Sage One Web Request: ", webex);
            string text;

            using (var sr = new StreamReader(webex.Response.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }

            result = null;
            throw new Exception("Web Exception thrown processing Sage One Request", webex);
        }
        catch (Exception ex)
        {
            Logger.Error("Exception while processing Sage One Web Request: ", ex);
            result = null;
            throw new Exception("Exception thrown processing Sage One Request", ex);
        }

        return result;
    }

任何與此問題的幫助將不勝感激! 謝謝!

編輯 :關於以下建議,遵循的實際URL路徑是“ api.sageone.com/accounts/v2/purchase_invoices”,而不是收到的錯誤中指出的requestpath。

根據錯誤響應,請求路徑看起來不正確。 它顯示“ requestPath”:“ / api / v2 / purchase_invoices”

但是文檔顯示/ accounts / v2 / purchase_invoices

您正在過帳到/purchase_invoices ,但您的參數僅用於expense 請使用purchase_invoice名稱包裝您的參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM