“你好,亚马逊!” - 向亚马逊产品 API 发送第一个请求
这个演示程序可以让你验证你的密钥是否有效,以及是否可以格式化请求。
引言
这篇文章是一个简单的“Hello”类型的程序,用于实例化和解释对 Amazon ECommerce Services 的一个简单请求。
背景
为了发起请求,你只需要提供你自己的 AWSAccessKeyID。你可以在 http://aws.amazon.com/ 免费获取一个。Amazon 提供了在 http://docs.amazonwebservices.com/AWSECommerceService/2009-01-06/GSG/index.html?ImplementinganA2SRequest.html 执行类似测试的代码,但它无法直接编译,并且没有展示在无需处理原始 XML 的情况下,使用响应信息中提供的便捷功能。
使用代码
为了使用产品 API,你需要将其作为服务引用添加到你的项目中。在解决方案资源管理器中,右键单击“引用”并选择“添加服务引用...”。使用 http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl 作为地址。在任何你想访问服务中的完整对象和方法的地方添加 using 引用。Web 服务非常棒 - 两个步骤,你就可以使用 IntelliSense 来浏览功能。
要从 API 获取信息,你首先需要创建一个请求。该请求附加到一个搜索中,该搜索只是一个包装器,用于一个或多个搜索,它还包含 AWSAccessKeyID
。最后,打开一个端口并使用搜索作为参数调用 ItemSearch
。
// In order to find information about an item we need at least one search request
// This search request (and any others) is attached to a search
// finally the search is submitted on a port and returns a response
// Create the request object
ItemSearchRequest request = new ItemSearchRequest();
// Fill request object with request parameters
request.ResponseGroup = new string[] { "ItemAttributes" };
// Set SearchIndex to All and use the scanned EAN
// as the keyword, this should generate a single response
request.SearchIndex = "All";
request.Keywords = txtLookupEAN.Text;
// Make the item search
ItemSearch search = new ItemSearch();
// It is ABSOLUTELY CRITICAL that you change
// the AWSAccessKeyID to YOUR uniqe value
// Signup for an account (and AccessKeyID) at http://aws.amazon.com/
search.AWSAccessKeyId = "[INSERT YOUR ACCESS ID HERE]";
// Set the request on the search wrapper - multiple requests
// can be submitted on one search
search.Request = new ItemSearchRequest[] { request };
// Make the port
AWSECommerceServicePortTypeClient port =
new AWSECommerceServicePortTypeClient();
//Send the request, store the response and display some of the results
ItemSearchResponse response = port.ItemSearch(search);
关注点
在搜索信息时的一个小困难是,虽然 Amazon 一直在重命名电子商务服务技术,但原始名称仍然被广泛使用。最初,它被称为 ECS(电子商务服务),然后被命名为“Amazon Associates Web Service”,现在正式名称为“Product Advertising API”。但是,基础 WSDL 文档仍然命名为 AWSECommerceService.wsdl。
历史
目前还没有。