c# - 使用 .NET 连接到亚马逊

标签 c# .net amazon-web-services web-services

我正试图通过像这样连接到亚马逊来返回一条数据:

using System;
using System.Collections.Generic;
using System.Text;
using AmazonProductAdvtApi;
using Microsoft.Web.Services3.Addressing;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Net;

namespace AmazonProductAdvtApi
{
    class HmacSample
    {
        // Note: protocol must be https for signed SOAP requests.
        const String DESTINATION    = "https://ecs.amazonaws.com/onca/soap?Service=AWSECommerceService";

        // Set your AWS Access Key ID and AWS Secret Key here.
        // You can obtain them at:
        // http://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key
        const String MY_AWS_ID = "MY_SECRET"; //yes it's there in  the real code
        const String MY_AWS_SECRET = "ANOTHER_SECRET"; //yes it's there in the real code

        // Select an item you wish to inspect and provide it's ASIN here:
        const String ITEM_ID = "0345505344";

        static void Main(string[] args)
        {
            // If you are using a debugging proxy like Fiddler to capture SOAP
            // envelopes, and you get SSL Certificate warnings, uncomment the 
            // following line:
            // ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

            // Create an ItemLookup request, with ResponseGroup "Small"
            // and using the item ID provided above.
            ItemLookup itemLookup = new ItemLookup();
            itemLookup.AWSAccessKeyId = MY_AWS_ID;

            ItemLookupRequest itemLookupRequest = new ItemLookupRequest();
            itemLookupRequest.ItemId = new String[] { ITEM_ID };
            itemLookupRequest.ResponseGroup = new String[] { "Small", "AlternateVersions" };
            itemLookup.Request = new ItemLookupRequest[] { itemLookupRequest };

            // create an instance of the serivce
            AWSECommerceService api = new AWSECommerceService();

            // set the destination
            api.Destination = new Uri(DESTINATION);

            // apply the security policy, which will add the require security elements to the
            // outgoing SOAP header
            AmazonHmacAssertion amazonHmacAssertion = new AmazonHmacAssertion(MY_AWS_ID, MY_AWS_SECRET);
            api.SetPolicy(amazonHmacAssertion.Policy());

            // make the call and print the title if it succeeds
            try
            {
                ItemLookupResponse itemLookupResponse = api.ItemLookup(itemLookup);
                Item item = itemLookupResponse.Items[0].Item[0];
                System.Console.WriteLine(item.ItemAttributes.Title);
            }
            catch (Exception e)
            {

                System.Console.Error.WriteLine(e);
            }

            // we're done
            System.Console.WriteLine("HMAC sample finished. Hit Enter to terminate.");
            System.Console.ReadLine();
        }
    }
}

我收到以下异常。

{"WSE101: An asynchronous operation raised an exception."}
"Microsoft.Web.Services3.AsynchronousOperationException: WSE101: An asynchronous operation raised an exception. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.

Server stack trace: 
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.Web.Services3.Messaging.SoapHttpTransport.Send(SoapEnvelope message, EndpointReference destination, SoapHttpChannelOptions options)
   at Microsoft.Web.Services3.Messaging.SoapHttpOutputChannel.Send(SoapEnvelope message)
   at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.PrivateProcessMessage(RuntimeMethodHandle md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
   at System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.EndInvokeHelper(Message reqMsg, Boolean bProxyCase)
   at System.Runtime.Remoting.Proxies.RemotingProxy.Invoke(Object NotUsed, MessageData& msgData)
   at Microsoft.Web.Services3.Messaging.SoapOutputChannel.SendDelegate.EndInvoke(IAsyncResult result)
   at Microsoft.Web.Services3.Messaging.SoapOutputChannel.EndSend(IAsyncResult result)
   at Microsoft.Web.Services3.Messaging.SoapSender.EndSend(IAsyncResult result)
   at Microsoft.Web.Services3.Messaging.SoapClient.SoapClientAsyncResult.OnSendComplete(IAsyncResult result)
   --- End of inner exception stack trace ---
   at Microsoft.Web.Services3.AsyncResult.End(IAsyncResult result)
   at Microsoft.Web.Services3.Messaging.SoapClient.SendRequestResponse(String methodname, SoapEnvelope envelope)
   at Microsoft.Web.Services3.Messaging.SoapClient.SendRequestResponse(String methodname, Object obj)
   at AmazonProductAdvtApi.AWSECommerceService.ItemLookup(ItemLookup ItemLookup1) in C:\\Documents and Settings\\agordon\\My Documents\\AmazonProductAdvtApiSampleCSharpSoap\\Client\\src\\AmazonProductAdvtApi.cs:line 218
   at AmazonProductAdvtApi.HmacSample.Main(String[] args) in C:\\Documents and Settings\\agordon\\My Documents\\AmazonProductAdvtApiSampleCSharpSoap\\Sample\\src\\HmacSample.cs:line 88"

你能帮忙解决这个问题吗?我不明白我做错了什么。我正在使用来自亚马逊网站的示例来检索产品信息。它来自他们的产品广告 API。

有谁知道他们是否有更多支持和新的东西?

最佳答案

您是否已将 id 和 secret 更改为您的 aws id/secret?

    // Set your AWS Access Key ID and AWS Secret Key here.
    // You can obtain them at:
    // http://aws-portal.amazon.com/gp/aws/developer/account/index.html?action=access-key
    const String MY_AWS_ID = "secret";
    const String MY_AWS_SECRET = "anothersecret";

关于c# - 使用 .NET 连接到亚马逊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6380679/

相关文章:

c# - 如何同步.NET中的并行 Action ?

amazon-web-services - JMESPath 过滤器表达式中的引用父元素

c# - 是否可以等到其他线程处理输入消息发布到它?

c# - 将模型转换为 MVC 测试中的列表

c# - 如何将类方法的值返回到调用它的相应页面

c# - C# 'for' 循环中的多重初始化

c# - Expression.Assign 返回 Func 而不是 Action

amazon-web-services - 在 AWS 上测试 Lambda 函数时遇到问题

amazon-web-services - Amazon ECS 任务定义 - CPU 单元和内存 - 将容器设置为使用 100% 的 EC2 可用资源

c# - 枚举整个 LinqToSql 对象