我正在尝试使用以下代码发布实现
我正在使用 json 将其转换为 xml,因为我的 json 是
var ShipJson = {
"AmazonEnvelope": {
"-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"-xsi:noNamespaceSchemaLocation": "amznenvelope.xsd",
"Header": {
"DocumentVersion": "1.01",
"MerchantIdentifier": MRChID
},
"MessageType": "OrderFulfillment",
"Message": {
"MessageID": "1",
"OrderFulfillment": {
"MerchantOrderID" : OrderId,
"MerchantFulfillmentID":"1234567",
"FulfillmentDate": "2017-06-20T00:36:33-08:00",
"FulfillmentData": {
"CarrierName": "UPS",
"ShippingMethod": "Second Day",
"ShipperTrackingNumber": "1234567890"
},
"Item": {
"MerchantOrderItemID":"1234567",
"MerchantFulfillmentItemID": "1234567",
"Quantity": "2"
}
}
}
}
};
我正在将此 json 转换为 xml,然后使用加密 js 转换为 bae64-md5
var xml = cm.util.Formatter.json2xml(ShipJson);
var ContentMD5Value = CryptoJS.MD5(xml,true);
var MD5Base64 = CryptoJS.enc.Base64.stringify(ContentMD5Value);
var that = this;
var uri = "/";
var today = new Date();
time = today.toISOString();
var parameters = {
"AWSAccessKeyId":;kjuhjfgd,
"Action":"SubmitFeed",
"ContentMD5Value": MD5Base64,
"FeedType":"_POST_ORDER_FULFILLMENT_DATA_",
"Merchant":lgtjljtd,
"PurgeAndReplace":"false",
"SignatureMethod":"HmacSHA256",
"SignatureVersion":"2",
"Timestamp": time,
"Version":"2009-01-01"
};
var parameters1 = $.param(parameters);
var messageToEncrypt = sap.ui.getCore.method+"\n"+sap.ui.getCore.host+"\n"+ uri +"\n"+parameters1;
var hash = CryptoJS.HmacSHA256(messageToEncrypt, sap.ui.getCore().secreteKey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var parameters100 = {
"AWSAccessKeyId":jhfjfzhkhgs,
"Action":"SubmitFeed",
"Merchant":gjdhjdgfzgd,
"SignatureVersion":"2",
"Timestamp": time,
"Version":"2009-01-01",
"ContentMD5Value": MD5Base64,
"Signature":hashInBase64,
"SignatureMethod":"HmacSHA256",
"FeedType":"_POST_ORDER_FULFILLMENT_DATA_",
"PurgeAndReplace":"false"
};
var parameters101 = $.param( parameters100 );
var mwsRequest = sap.ui.getCore.protocol+"://"+sap.ui.getCore.host;
var odatamodel = new sap.ui.model.odata.ODataModel(mwsRequest, {
headers: {
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"
}
});
sap.ui.getCore().setModel(odatamodel);
OData.request({
requestUri: mwsRequest+ "/?" + parameters101,
method: "POST",
headers: {
},
data: {}
},
function(data, response) {
},
function(err,response) {
var res = err.response.body;
}
);
但是我遇到了 MD5Content 值不匹配的错误
"<?xml version="1.0"?>
<ErrorResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/"><Error><Type>Sender</Type><Code>ContentMD5DoesNotMatch</Code><Message>the Content-MD5 HTTP header you passed for your feed did not match the Content-MD5 we calculated for your feed</Message><Detail/></Error><RequestID>43637524-65a0-42be-bdfc-3d78290e8810</RequestID></ErrorResponse>"
请帮帮我,我是按照错误的方法还是遗漏了什么或者我的 MD5 库有误?
您计算 MD5 的方式似乎是正确的。但是,您正在发送
ContentMD5Value
作为请求的一部分。我不确定这是否是一个可以接受的替代方案,我看到的所有代码都发送了一个 http 标头:Content-MD5: stringWithMD5inBase64
以及 Content-Type: text/xml
.
我不确定所有 sap.ui 的东西在做什么(而且我对 JavaScript 不是很擅长),但我猜你需要更改 OData.request 中的
headers
元素。但话又说回来,我没看到你真的发送过xml
的内容?