我在Sage One API中創建「Purchase Invoice」時遇到問題。無論我對傳入的數據所做的更改如何,我似乎都會收到500內部服務錯誤,並且沒有包含任何有意義信息的詳細響應。無論輸入什麼數據(只有errorcode字段中的GUID和服務器時間更改),響應總是相同的。迴應如下:Sage One API在創建付款發票時返回500內部服務錯誤
{
"$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> bodyParams,string token,string signingSecret) { string result;
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」,而不是接收到的錯誤中記錄的請求路徑。
我修改我原來的意見,然而,很明顯這裏,所謂的實際URL是「https://api.sageone.com/accounts/v2/purchase_invoices」,如文檔中表示。我不知道API爲什麼返回該URL。 –