2011-04-26 93 views
3

我試圖在亞馬遜的產品數據庫張貼在亞馬遜Web服務示例代碼頁亞馬遜產品廣告API簽名問題

AWSECommerceService ecs = new AWSECommerceService(); 

// Create ItemSearch wrapper 
ItemSearch search = new ItemSearch(); 
search.AssociateTag = "ABC"; 
search.AWSAccessKeyId = "XYZ"; 

// Create a request object 
ItemSearchRequest request = new ItemSearchRequest(); 

// Fill request object with request parameters 
request.ResponseGroup = new string[] { "ItemAttributes" }; 

// Set SearchIndex and Keywords 
request.SearchIndex = "All"; 
request.Keywords = "The Shawshank Redemption"; 

// Set the request on the search wrapper 
search.Request = new ItemSearchRequest[] { request }; 

try 
{ 
    //Send the request and store the response 
    //in response 

    ItemSearchResponse response = ecs.ItemSearch(search); 
    gvRes.DataSource = response.Items; 
} 
catch (Exception ex) 
{ 
    divContent.InnerText = ex.Message; 
} 

,並收到以下錯誤

要求必須將以下代碼搜索包含參數 簽名。

和亞馬遜文檔不清楚如何簽署請求。

任何想法如何使它工作?

THX

+1

退房的文檔在這裏:http://docs.amazonwebservices.com/AWSECommerceService/2010-11-01/DG/RequestAuthenticationArticle.html – Denise 2011-04-26 16:30:00

+0

,有一個代碼示例太:http://aws.amazon.com/code/Product-Advertising-API/3941 – Rup 2011-04-26 17:35:01

回答

2

有一個輔助類的REST稱爲SignedRequestHelper。

你叫它像這樣:

SignedRequestHelper helper = 
     new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION); 
requestUrl = helper.Sign(querystring); 

必須有一個類似在上述鏈接SOAP調用。

+0

任何人都可以請告訴我,我從哪裏得到這個'SignedRequestHelper'類?在C#中... – 2016-12-19 07:53:54

1

嘗試這一個..我希望它會幫助..我嘗試它的工作..請與他人分享。

上下載http://www.falconwebtech.com/post/Using-WCF-and-SOAP-to-Send-Amazon-Product-Advertising-API-Signed-Requests

我們需要更新服務引用,請在app.config中,Program.cs中,並reference.cs變化不大的示例代碼。

app.config: (1.) appSettings tag; 分配accessKeyId和secretKey值, 添加。 (2.)行爲標記 - > endpointBehaviors標記 - >行爲標記 - > signingBehavior標記; 分配accessKeyId和secretKey值。 (3.)綁定標籤 - > basicHttpBinding標籤; (可選) 刪除除AWSECommerceServiceBindingNoTransport 和AWSECommerceServiceBindingTransport之外的綁定標記。 (4.)客戶端標籤; 刪除AWSECommerceServiceBindingTransport以外的端點標記。

program.cs: add itemSearch.AssociateTag = ConfigurationManager.AppSettings [「associateTag」]; ItemSearchResponse響應之前= amazonClient.ItemSearch(itemSearch);

reference.cs:(在服務引用打開文件夾使用的Visual Studio) 變化私人ImageSet [] [] imageSetsField;私人ImageSet [] imageSetsField; 變化公共ImageSet [] [] ImageSets {...}公共ImageSet [] ImageSets {...}

終於可以運行我們的程序,它會工作。祝你好運..

注意:會有1個警告(無效的子元素簽約行爲),我想我們可以忽略它,或者如果您有任何解決方案,請分享.. ^^訴。

3

我轉錄this VB代碼和它的作品對我來說

添加服務引用並將其命名爲亞馬遜

http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl 

進入您的項目託管文件夾,打開服務引用文件夾並打開Reference.cs,然後全部更換[] []與[],下一次出現打開AWSECommerceService.wsdl並找到

<xs:element minOccurs="0" maxOccurs="unbounded" name="ImageSets"> 

<xs:element minOccurs="0" maxOccurs="1" name="ImageSets"> 

更換添加以下內容,你需要手動引用某些DLL

using System.Security.Cryptography; 
using System.ServiceModel; 
using System.ServiceModel.Channels; 
using System.ServiceModel.Dispatcher; 
using System.ServiceModel.Description; 
using System.Text.RegularExpressions; 
using System.Xml; 
using System.IO; 
using System.Runtime.Serialization; 
using AmazonApiTest.Amazon; //instead of AmazonApiTest use your project name 

第一各個接口實現

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 System.ServiceModel.Channels.Message request, IClientChannel channel) 
    { 
     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); 

     Byte[] secretKeyBytes = Encoding.UTF8.GetBytes(secretKey); 
     HMAC hmacSha256 = new HMACSHA256(secretKeyBytes); 
     Byte[] hashBytes = hmacSha256.ComputeHash(bytesToSign); 
     String signature = Convert.ToBase64String(hashBytes); 

     request.Headers.Add(new AmazonHeader("AWSAccessKeyId", accessKeyId)); 
     request.Headers.Add(new AmazonHeader("Timestamp", timestamp)); 
     request.Headers.Add(new AmazonHeader("Signature", signature)); 
     return null; 
    } 

    void IClientMessageInspector.AfterReceiveReply(ref System.ServiceModel.Channels.Message Message, Object correlationState) 
    { 
    } 
} 

public class AmazonSigningEndpointBehavior : IEndpointBehavior 
{ 
    private string accessKeyId = ""; 
    private string secretKey = ""; 

    public AmazonSigningEndpointBehavior(string accessKeyId, string secretKey) 
    { 
     this.accessKeyId = accessKeyId; 
     this.secretKey = secretKey; 
    } 

    public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) 
    { 
     clientRuntime.ClientMessageInspectors.Add(new AmazonSigningMessageInspector(accessKeyId, secretKey)); 
    } 

    public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatched) 
    { 
    } 

    public void Validate(ServiceEndpoint serviceEndpoint) 
    { 
    } 

    public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParemeters) 
    { 
    } 
} 

public class AmazonHeader : MessageHeader 
{ 
    private string m_name; 
    private string value; 

    public AmazonHeader(string name, string value) 
    { 
     this.m_name = name; 
     this.value = value; 
    } 

    public override string Name 
    { 
     get { return m_name; } 
    } 
    public override string Namespace 
    { 
     get { return "http://security.amazonaws.com/doc/2007-01-01/"; } 
    } 
    protected override void OnWriteHeaderContents(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion) 
    { 
     writer.WriteString(value); 
    } 
} 

現在您使用生成的代碼以這種方式

ItemSearch search = new ItemSearch(); 
search.AssociateTag = "YOUR ASSOCIATE TAG"; 
search.AWSAccessKeyId = "YOUR AWS ACCESS KEY ID";   
ItemSearchRequest req = new ItemSearchRequest(); 
req.ResponseGroup = new string[] { "ItemAttributes" }; 
req.SearchIndex = "Books"; 
req.Author = "Lansdale"; 
req.Availability = ItemSearchRequestAvailability.Available; 
search.Request = new ItemSearchRequest[]{req}; 

Amazon.AWSECommerceServicePortTypeClient amzwc = new Amazon.AWSECommerceServicePortTypeClient(); 
amzwc.ChannelFactory.Endpoint.EndpointBehaviors.Add(new AmazonSigningEndpointBehavior("ACCESS KEY", "SECRET KEY")); 

ItemSearchResponse resp = amzwc.ItemSearch(search); 

foreach (Item item in resp.Items[0].Item) 
    Console.WriteLine(item.ItemAttributes.Author[0] + " - " + item.ItemAttributes.Title); 
+0

爲我工作,謝謝。 – Kniganapolke 2014-04-02 13:14:58

相關問題