2012-02-25 4 views
2

아마존 ECS (전자 상거래 서비스) API의 문제를 해결하기 위해 하루 종일 노력했습니다.Amazon 제품 광고 API ItemSearch Null 반환

나는 .NET 4.0 및 WCF

내가있는 AccessKeyID 및 SecretyKeyID을 제외하고는 예로부터 코드에서 아무것도 변경되지 않은

http://aws.amazon.com/code/Product-Advertising-API/3941

와 SOAP 요청을 보내기 위해 자신의 사이트에있는 예제를 다운로드 한 설정 파일.

호출하는 코드는 다음과 같습니다 :

 // 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()에 대한 호출은 null 객체를 반환합니다. 더 자세히 살펴보면, 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

PM> Install-Package Nager.AmazonProductAdvertising 

nuget

nuget을 통해 라이브러리도 사용할 수 있습니다. 해결책을 찾는 것은 나를 여기에서 이끌었다. ItemSearch.AssociateTag을 ItemSearch를 호출하기 전에 설정했습니다 ... 나는 또한 Fiddler와 AfterReceiveReply에서 올바른 결과를 봅니다. Debug-> Exceptions를 켜면 AmazonSOAP.XmlSerializers에 대해 FileNotFoundException이 발생합니다. 이것에 대해 진전을 보았습니까? –

+0

동일한 문제가 발생했습니다. 해결책을 얻었을 때 알려주십시오 – ikbal

답변

4

내 문제는 내가 관련 태그를 놓치고 있다는 것입니다.

itemSearch.AssociateTag = "213";

확실히 생성 된 코드에 문제가있다, ItemSearchResponse 코드에 의해 노출되지 않는 오류 컬렉션을 포함. 그것은 올바른 방향으로 나를 가리키는 감찰관의 생생한 메시지를 보는 것입니다.

+1

아마존은 끔찍한 API를 만들 수있는 요령을 가지고 있습니다 ... 누군가이 라이브러리를 사용했다면 http://flyingpies.wordpress.com/2009/08/ 13/signing-amazon-product-advertising-api-cwcf-part-2/Associate 태그를 추가 할 때 WSDL도 변경해야합니다 - https://forums.aws.amazon.com/thread.jspa?threadID = 72429 – kape123

2

실종 된 연관 태그에 대한 답은 저에게 도움이되었지만 WSDL URL과 끝점 주소가 등록한 Amazon 사이트와 일치하는지 확인해야했습니다. 영국 사이트에 등록 했으므로 사용해야합니다.

WSDL
Endpoint Address

0

Nager.AmazonProductAdvertising github 에서이 과제에 대한 현재 프로젝트. 나도 같은 샘플 프로젝트에서 동일한 문제가 있어요

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); 
관련 문제