gpt4 book ai didi

c# - 亚马逊产品广告 API ItemSearch 返回空值

转载 作者:行者123 更新时间:2023-11-30 20:06:43 24 4
gpt4 key购买 nike

我花了一整天的时间尝试解决 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)代码如下:

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)
{


}
}

我到处都看到这个问题,但没有人在任何地方发布修复程序。有人请帮我解决这个问题。

最佳答案

我的问题是我缺少关联标签。

itemSearch.AssociateTag = "213";

生成的代码肯定有问题,ItemSearchResponse 包含一个代码未公开的错误集合。正是通过查看检查器中的原始消息为我指明了正确的方向。

关于c# - 亚马逊产品广告 API ItemSearch 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9448589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com