我正在尝试为 GetAuthToken() 方法访问亚马逊网络服务。但每次我得到签名不正确的错误。每次我得到这个错误。找不到确切的原因
任何人都可以为此分享工作示例或看看我在这里做什么?
我的代码:
private void GetToken()
{
string secretKey = "my secret key";
string serviceUrl = "https://mws.amazonservices.com/Sellers/2011-07-01";
string CHARACTER_ENCODING = "UTF-8";
string ALGORITHM = "HmacSHA256";
// Create set of parameters needed and store in a map
Dictionary<string, string> parameters = new Dictionary<string,string>();
// Add required parameters. Change these as needed.
parameters.Add("AWSAccessKeyId", "my access id");
parameters.Add("Action","GetAuthToken");
parameters.Add("MWSAuthToken", "");
parameters.Add("SellerId","my seller id");
parameters.Add("SignatureMethod", ALGORITHM);
parameters.Add("SignatureVersion",("2");
parameters.Add("Timestamp", "2015-07-06T16:03:00Z");
parameters.Add("Version", "2011-07-01");
// Format the parameters (without the signature parameter)
string formattedParameters = calculateStringToSignV2(parameters, serviceUrl);
string signature = signNew(formattedParameters, secretKey);
// Add signature to the parameters
parameters.Add("Signature", signature);
formattedParameters = calculateStringToSignV2(parameters, serviceUrl);
string querystring = formattedParameters.Substring(formattedParameters.IndexOf("AWSAccessKeyId"));
// Setup the HTTP request.
HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl + "?" + querystring);
objWebRequest.Method = "POST";
objWebRequest.AllowAutoRedirect = true;
objWebRequest.ContentLength = querystring.Length;
// Post to the login form.
StreamWriter swRequestWriter = new StreamWriter(objWebRequest.GetRequestStream());
swRequestWriter.Write(querystring);
swRequestWriter.Close();
HttpWebResponse objWebResponse = null;
try
{
// Get the response.
objWebResponse =
(HttpWebResponse)objWebRequest.GetResponse();
}
catch(WebException e)
{
}
// Read the response
StreamReader srResponseReader = new
StreamReader(objWebResponse.GetResponseStream());
string strResponseData = srResponseReader.ReadToEnd();
srResponseReader.Close();
responseTxt.Text =strResponseData;
private static string calculateStringToSignV2(Dictionary<string, string> _Parameters, string serviceUrl)
{
Uri endpoint = new Uri(serviceUrl.ToLower());
StringBuilder data = new StringBuilder();
data.Append("POST\n");
data.Append(endpoint.Host);
data.Append("\n/");
data.Append("\n");
SortedDictionary<string, string> sorted = new SortedDictionary<string, string>();
foreach (KeyValuePair<String, String> pair in _Parameters)
{
sorted.Add(pair.Key, pair.Value);
}
if (sorted.Count > 0)
{
//data.Append("?");
bool first = true;
foreach (KeyValuePair<String, String> pair in sorted)
{
if (!first)
{
data.Append("&");
}
else
{
first = false;
}
data.Append((pair.Key));
data.Append("=");
data.Append((pair.Value));
}
}
return data.ToString();
}
}
private static String signNew(string parameters, string secretKey)
{
string data = parameters;
HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey));
hmac.Initialize();
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] signature = hmac.ComputeHash(bytes);
string signatureBase64 = Convert.ToBase64String(signature);
return signatureBase64;
}
private static String urlEncodeNew(String rawValue) {
string value = (rawValue == null) ? "" : rawValue;
string encoded = null;
try {
encoded = System.Web.HttpUtility.UrlEncode(rawValue, Encoding.UTF8).Replace("+", "%20")
.Replace("*", "%2A")
.Replace("%7E", "~");
} catch (Exception e) {
}
return encoded;
}