我花了一整天时间尝试修复Amazon ECS(电子商务服务)API的问题。
我已经在他们的网站上下载了用于使用.NET 4.0和WCF发送SOAP请求的示例
http://aws.amazon.com/code/Product-Advertising-API/3941
除了配置文件中的AccessKeyID和SecretyKeyID之外,我没有更改代码中的任何内容。
调用代码如下所示:
// Instantiate Amazon ProductAdvertisingAPI client
AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();
// prepare an ItemSearch request
ItemSearchRequest request = new ItemSearchRequest();
request.SearchIndex = "Books";
request.Title = "WCF";
request.ResponseGroup = new string[] { "Medium"};
ItemSearch itemSearch = new ItemSearch();
itemSearch.Request = new ItemSearchRequest[] { request };
request.Condition = Condition.All;
itemSearch.AssociateTag = "";
itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
// send the ItemSearch request
ItemSearchResponse response = amazonClient.ItemSearch(itemSearch);
if (response != null)
{
// write out the results from the ItemSearch request
foreach (var item in response.Items[0].Item)
{
Console.WriteLine(item.ItemAttributes.Title);
}
}
Console.WriteLine("done...enter any key to continue>");
Console.ReadLine();
对ItemSearch()的调用返回一个空对象。我进一步研究了这一点,发现在AmazongSigningMessageInspector类中,AfterReceiveReply()方法显示正在返回带有结果的正确SOAP XML响应,因此我知道它正在调用服务并正确返回。出于某种原因,虽然我留下了一个NULL ItemSearch对象。
我的课程代码如下:
class AmazonSigningBehaviorExtensionElement : BehaviorExtensionElement
{
public AmazonSigningBehaviorExtensionElement()
{
}
public override Type BehaviorType
{
get
{
return typeof(AmazonSigningEndpointBehavior);
}
}
protected override object CreateBehavior()
{
return new AmazonSigningEndpointBehavior(AccessKeyId, SecretKey);
}
[ConfigurationProperty("accessKeyId", IsRequired = true)]
public string AccessKeyId
{
get { return (string)base["accessKeyId"]; }
set { base["accessKeyId"] = value; }
}
[ConfigurationProperty("secretKey", IsRequired = true)]
public string SecretKey
{
get { return (string)base["secretKey"]; }
set { base["secretKey"] = value; }
}
}
public class AmazonSigningEndpointBehavior : IEndpointBehavior {
private string _accessKeyId = "";
private string _secretKey = "";
public AmazonSigningEndpointBehavior()
{
this._accessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
this._secretKey = ConfigurationManager.AppSettings["secretKey"];
}
public AmazonSigningEndpointBehavior(string accessKeyId, string secretKey) {
this._accessKeyId = accessKeyId;
this._secretKey = secretKey;
}
public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) {
clientRuntime.MessageInspectors.Add(new AmazonSigningMessageInspector(_accessKeyId, _secretKey));
}
public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher) { return; }
public void Validate(ServiceEndpoint serviceEndpoint) { return; }
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters) { return; }
}
public class AmazonSigningMessageInspector : IClientMessageInspector {
private string _accessKeyId = "";
private string _secretKey = "";
public AmazonSigningMessageInspector(string accessKeyId, string secretKey) {
this._accessKeyId = accessKeyId;
this._secretKey = secretKey;
}
public object BeforeSendRequest(ref Message request, IClientChannel channel) {
// prepare the data to sign
string operation = Regex.Match(request.Headers.Action, "[^/]+$").ToString();
DateTime now = DateTime.UtcNow;
string timestamp = now.ToString("yyyy-MM-ddTHH:mm:ssZ");
string signMe = operation + timestamp;
byte[] bytesToSign = Encoding.UTF8.GetBytes(signMe);
// sign the data
byte[] secretKeyBytes = Encoding.UTF8.GetBytes(_secretKey);
HMAC hmacSha256 = new HMACSHA256(secretKeyBytes);
byte[] hashBytes = hmacSha256.ComputeHash(bytesToSign);
string signature = Convert.ToBase64String(hashBytes);
// add the signature information to the request headers
request.Headers.Add(new AmazonHeader("AWSAccessKeyId", _accessKeyId));
request.Headers.Add(new AmazonHeader("Timestamp", timestamp));
request.Headers.Add(new AmazonHeader("Signature", signature));
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
}
我到处都看到过这个问题,但没有人在任何地方发布过修复程序。有人请帮帮我。
答案 0 :(得分:4)
我的问题是我错过了副标签。
itemSearch.AssociateTag = "213";
生成的代码肯定存在问题,ItemSearchResponse包含一个错误集合,不会被代码公开。通过查看检查员中的原始信息,我指出了正确的方向。
答案 1 :(得分:2)
关于缺少关联标记的答案对我有帮助,但我还必须确保WSDL URL和端点地址与您在其下注册的Amazon网站相匹配。我在英国网站注册,所以我需要使用。
答案 2 :(得分:1)
您可以在Nager.AmazonProductAdvertising github上找到此作业的当前项目。该库也可通过nuget
获取<强>的NuGet 强>
PM> Install-Package Nager.AmazonProductAdvertising
示例强>
var authentication = new AmazonAuthentication();
authentication.AccessKey = "accesskey";
authentication.SecretKey = "secretkey";
var wrapper = new AmazonWrapper(authentication, AmazonEndpoint.US);
var result = wrapper.Search("canon eos", AmazonSearchIndex.Electronics, AmazonResponseGroup.Large);
答案 3 :(得分:0)
我编译并发布了此示例的修复程序。请到这里: https://forums.aws.amazon.com/message.jspa?messageID=440527#440527