Last active
August 29, 2015 14:15
-
-
Save nakaji/8e6efe6297b84d8aef74 to your computer and use it in GitHub Desktop.
Amazon Product Advertising API Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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