Skip to content

Instantly share code, notes, and snippets.

@nakaji
Last active August 29, 2015 14:15
Show Gist options
  • Save nakaji/8e6efe6297b84d8aef74 to your computer and use it in GitHub Desktop.
Save nakaji/8e6efe6297b84d8aef74 to your computer and use it in GitHub Desktop.
Amazon Product Advertising API Sample
using System;
using System.Linq;
using System.Xml.Linq;
namespace AmazonProductAdvtApi
{
class ItemLookupSample
{
private const string MY_AWS_ACCESS_KEY_ID = "{Access Key ID}";
private const string MY_AWS_SECRET_KEY = "{Secret Access Key}";
private const string DESTINATION = "ecs.amazonaws.jp";
private const string ASSOCIATE_TAG = "{アソシエイトタグ}";
public static void Main()
{
var helper = new SignedRequestHelper(MY_AWS_ACCESS_KEY_ID, MY_AWS_SECRET_KEY, DESTINATION, ASSOCIATE_TAG);
var keyword = "納品のない受託開発";
String requestString = "Service=AWSECommerceService"
+ "&Version=2009-03-31"
+ "&Operation=ItemSearch"
+ "&SearchIndex=Books"
+ "&ResponseGroup=Medium"
+ "&Keywords=" + keyword
;
var requestUrl = helper.Sign(requestString);
var title = FetchTitle(requestUrl);
Console.WriteLine("Keyword=\"" + keyword + "\"; Title=\"" + title + "\"");
Console.WriteLine();
Console.WriteLine("Hit Enter to end");
Console.ReadLine();
}
private static string FetchTitle(string url)
{
XDocument xml = XDocument.Load(url);
var ns = xml.Root.Name.Namespace;
var errorMessageNodes = xml.Descendants(ns + "Message").ToList();
if (errorMessageNodes.Any())
{
var message = errorMessageNodes[0].Value;
return "Error: " + message;
}
var title = xml.Descendants(ns + "Title").First();
return title.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment