我是AWS的新手,我正在尝试通过UPC在我的UWP应用程序中查找项目。我在向请求URL添加签名时遇到问题。我一直收到“我们计算的请求签名与您提供的签名不符。”
有任何打电话给亚马逊的经验吗?在大多数地方,api文档并不是真正针对C#。
public async void ItemLookup(string itemID)
{
Dictionary<string, string> fields = new Dictionary<string, string>();
fields.Add("AssociateTag", associateTag);
fields.Add("Condition", "All");
fields.Add("IdType", "UPC");
fields.Add("ItemId", itemID);
fields.Add("Operation", "ItemLookup");
fields.Add("ResponseGroup", "Large");
fields.Add("SearchIndex", "All");
fields.Add("Service", "AWSECommerceService");
// fields.Add("Timestamp", Uri.EscapeDataString(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sssZ")));
// Build Url for signing
string url = string.Empty;
foreach (var pair in fields)
{
url += "&" + pair.Key + "=" + pair.Value;
}
url = Uri.EscapeUriString(endpoint
+ "AWSAccessKeyId=" + accessKeyId
+ url);
// Add Timestamp
url += "&Timestamp=" + Uri.EscapeDataString(DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", CultureInfo.InvariantCulture));
// URL http://webservices.amazon.co.uk/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Condition=All&IdType=UPC&ItemId=786936724943&Operation=ItemLookup&ResponseGroup=Large&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-11-22T11%3A34%3A42.602Z
// SCRATCHPAD http://webservices.amazon.co.uk/onca/xml?AWSAccessKeyId=REMOVED&AssociateTag=REMOVED&Condition=All&IdType=UPC&ItemId=786936724943&Operation=ItemLookup&ResponseGroup=Large&SearchIndex=All&Service=AWSECommerceService&Timestamp=2017-11-22T09%3A20%3A35.000Z
// &Signature=Lvqlenpx0wos4Hg6ZzSNHqOc1QwktXgt8nFHBTfTON4%3D
Byte[] secretBytes = UTF8Encoding.UTF8.GetBytes(secretKey);
HMACSHA256 hmac = new HMACSHA256(secretBytes);
Byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(url);
Byte[] hash = hmac.ComputeHash(dataBytes);
String signature = Convert.ToBase64String(hash);
// Full URL
string requestURL = url + "&Signature=" + signature;
HttpClient httpClient = new HttpClient();
HttpResponseMessage responseMessage = await httpClient.GetAsync(requestURL);
var response = await responseMessage.Content.ReadAsStringAsync();
}
}
我有来自amazon scratchpad的网址对我自己的网址进行了评论,它们是对齐的,但我不能100%确定我是否正确处理签名。
非常感谢任何帮助
凯文
答案 0 :(得分:1)
我还没有测试过以下内容。但是有关于你要求什么的文件。您是否检查了以下内容以创建签名?
首先,列出的整个流程here告诉我们您需要使用签名网址
其次,关于如何使用C#和.NET创建URL签名,您可以参考以下文档:http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CreateSignatureInCSharp.html
顺便说一下,您可以先查看示例code。
答案 1 :(得分:1)
您可以使用以下nuget包。
PM> Install-Package Nager.AmazonProductAdvertising
查找示例
var authentication = new AmazonAuthentication();
authentication.AccessKey = "accesskey";
authentication.SecretKey = "secretkey";
var wrapper = new AmazonWrapper(authentication, AmazonEndpoint.UK);
var result = wrapper.Lookup("B00BYPW00I");