Posting request to Amazon MWS services
I have been wasting few days on this issue already... The main problem is - I dont know how to POST data. I would like to start by getting OrderReferenceDetails. by looking at MWS Scratchpad (API Section: Off Amazon payments, Operation: GetOferenceDetails).
As I understand, first thing I should do, is to calculate signature. So by using this Amazon developer guide, and taking values in example in scratchpad, I created string, from which I calculated signature. This string looks like this
POST
mws.amazonservices.com
/offamazonpayments_sandbox/2013-01-01
Action=GetOrderReferenceDetails&AmazonOrderReferenceId=[_my order reference id_]&AWSAccessKeyId=[_my access key_]&SellerId=[_my seller id_]&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2014-11-04T12%3a15%3a38.1988397Z&Version=2013-01-01
So I hash this and get a signature, then I should add it to line of query parameters like &Signature=[_my clalculated signature]
Function I use to calculate signature:
private static String sign(String data, String secretKey)
{
String result = null;
KeyedHashAlgorithm algorithm = new HMACSHA256();
Encoding encoding = new UTF8Encoding();
algorithm.Key = encoding.GetBytes(secretKey);
result = Convert.ToBase64String(algorithm.ComputeHash(encoding.GetBytes(data.ToCharArray())));
return result;
}
Problem start here (I think). Scratchpad provides "data" to post, but I cant understand, how to use it (copy-paste from scratchpad):
POST /OffAmazonPayments_Sandbox/2013-01-01?AWSAccessKeyId=
&Action=GetOrderReferenceDetails
&SellerId=
&SignatureVersion=2
&Timestamp=2014-11-04T12%3A37%3A58Z
&Version=2013-01-01
&Signature=rEqRKP27Pklu%2BAmRLR%2ByRpUtfhNsVOWuGTQ7s%2FgkB2w%3D
&SignatureMethod=HmacSHA256
&AmazonOrderReferenceId= HTTP/1.1
Host: mws.amazonservices.com
x-amazon-user-agent: AmazonJavascriptScratchpad/1.0 (Language=Javascript)
Content-Type: text/xml
What is this? I am familiar of making request like this:
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/xml";
request.Host = "whatever host I need to use";
request.ContentLength = Encoding.UTF8.GetByteCount(dataAsString);
byte[] byteArray = Encoding.UTF8.GetBytes(dataAsString);
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
string responseAsString = streamReader.ReadToEnd();
}
}
catch (WebException exception)
{
HttpWebResponse exceptionalResponse = exception.Response as HttpWebResponse;
using (var streamReader = new StreamReader(exceptionalResponse.GetResponseStream()))
{
String responseAsString = streamReader.ReadToEnd();
}
}
Questions are:
Am I calculating signature properly?
How to construct web request?
Thanks for reading and trying to help!
EDIT:
Well, I finally got a response from MWS Amazon, that is not an error! Steps I did to make it work:
Construct string that will be signed by hand - order of query parameters is important.
Construct url where I will post data by hand - again, order is important, but different from string signed before. Keys in signed string should be sorted using lexicographic byte ordering. Well, I dont know what kind of animal is that, so thats why I did everything by hand, by example taking scratchpad.
链接地址: http://www.djcxy.com/p/6646.html上一篇: 该请求必须包含参数Signature
下一篇: 向亚马逊MWS服务发布请求