首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sage One API在创建付款发票时返回500内部服务错误

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

Stack Overflow用户
提问于 2016-04-29 16:15:25
回答 2查看 657关注 0票数 0

在Sage One API中创建“购买发票”时,我遇到了一个问题。不管我对传递的数据做了什么更改,我似乎得到了一个500个内部服务错误,并且没有包含任何有意义的信息的详细响应。响应总是相同的,不管我输入了什么数据(只有errorCode字段中的GUID和服务器时间的变化)。答复如下:

代码语言:javascript
复制
{
"$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”。

下面是调用的一些相关代码,再次记住基本架构是他们的,并进行了一些过程修改,以适应我当前的体系结构,而不影响实际调用:

代码语言:javascript
复制
    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请求的实际方法:

公共静态字符串创建(Uri baseUrl,List> bodyParams,string token,string signingSecret) { string结果;

代码语言:javascript
复制
        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",编辑:对于下面的建议,实际的路径是,而不是在收到的错误中注意到的请求路径。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-05 09:07:24

您正在向/purchase_invoices投递,但是您的参数是用于expense的。请使用purchase_invoice名称包装您的参数。

票数 0
EN

Stack Overflow用户

发布于 2016-05-03 16:04:17

根据错误响应,请求路径看起来不正确。它显示"requestPath":“/api/v2/购买发票”

但是文件显示/帐户/v2/购买发票

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36943105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档