Sage One API在创建付款发票时返回500内部服务错误

时间:2016-04-29 16:15:26

标签: c# api sage-erp sage-one

我在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"
      }
     }
  ]
}

我很肯定这不是授权问题,因为我在此之前获取并创建了其他数据类型。这包括创建采购发票所需的联系人。

我已按照self-service site提供的信息进行了操作。我也开始使用他们的bare bones 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请求的实际方法:

public static string Create(Uri baseUrl,List&gt; bodyParams,string token,string 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;
    }

对此问题的任何帮助将不胜感激!谢谢!

修改:关于下面的建议,所遵循的实际网址路径是“api.sageone.com/accounts/v2/purchase_invoices”,而不是收到的错误中记录的请求路径。

1 个答案:

答案 0 :(得分:0)

根据错误响应,请求路径看起来不正确。表明 &#34; requestPath&#34;:&#34; / api / v2 / purchase_invoices&#34;

但文档显示 /账户/ V2 / purchase_invoices