Amazon {Article001}

説明文の表示 ライブデモを見る▶
ようこそ「おカネか生きがいか」へ...

Amazon MWS APIのレスポンスをXML形式でブラウザに表示する (Amazon JP Version)

このサンプルでは ASP.NETのResponse.Write()メッソを使用して Amazon MWS APIのレスポンスをXML形式でブラウザに表示します。 ここでは、 MWSのGetMyPriceForSKU(), GetLowestOfferListingsForASIN(), GetLowestPriceOffersForASIN(), GetMatchingProductForId() の4種類のAPIを呼び出してレスポンスをXML形式で表示します。 GetMyPriceForSKU() APIに対しては リクエストパラメータにAmazonに登録した商品のSKUを設定します。 その他のAPIについては商品のASINを設定します。 ここでは、 日本のAmazon.co.jpに登録されている SKUとASINを使用します。 Amazon.com, Amazon.ca, Amazon.com.au に登録されているSKU, ASINのレスポンスを表示するときは、 Article002(Amazon US Version), Article003(Amazon CA Version), Article004(Amazon AU Version) の記事を参照してください。

作成手順

  1. VS2019を起動したら新規のWebフォームを作成します。
  2. VS2019のツールボックスからテキストボックス(TextBox)コントールをドラッグしたらWebフォームに3個を配置します。 これらのTextBoxにはAmazon.co.jpに登録されている商品のSKUを入力します。 TextBoxのIDプロパティを「txtSKU1」,「txtSKU2」,「txtSKU3」に書き換えます。 Columnsプロパティには「35」を設定します。 Textプロパティは空白にします。
  3. VS2019のツールボックスからボタン(Button)コントールをドラッグしたら、SKUのテキストボックスの下に配置します。 ButtonのIDプロパティは「btnGetMyPrice」に書き換えます。 Textプロパティに「GetMyPriceForSKU!」を設定します。 OnClickイベント「btnGetMyPrice_Click」を追加します。 BackColorプロパティに「Orange」を設定してボタンの背景色をオレンジ色にします。
  4. VS2019のツールボックスからラベル(Label)コントールをドラッグしたら、コマンドボタンの下に配置します。 Labelにはエラー等が発生したときにメッセージを表示します。 LabelのIDプロパティを「lblLog1」に書き換えます。 ForeColorプロパティを追加したら「Red」を設定してメッセージが赤色で表示されるようにします。
    
    SKU1: <asp:TextBox ID="txtSKU1" runat="server" Text="" Columns="35" />
    SKU2: <asp:TextBox ID="txtSKU2" runat="server" Text="" Columns="35" />
    SKU3: <asp:TextBox ID="txtSKU3" runat="server" Text="" Columns="35" />
                    
    <asp:Button ID="btnGetMyPrice" runat="server" BackColor="Orange" Text="GetMyPriceForSKU !" OnClick="btnGetMyPrice_Click" />
                    
    <asp:Label ID="lblLog1" runat="server" ForeColor="Red" Text="" />
    
  5. 同様の手順でASINを入力するテキストボックス3個とボタン3個そしてラベルを配置します。 TextBoxのIDプロパティは「txtASIN1」「txtASIN2」「txtASIN3」に書き換えます。 Columnsプロパティには「10」を設定します。 ButtonのIDプロパティを「btnGetPrice1」「btnGetPric2」「btnGetAttributes」に書き換えたら、 OnClickイベント「btnGetPrice1_Click」「btnGetPrice2_Click」「btnGetAttributes_Click」を追加します。 Textプロパティには「GetLowestOfferListingsForASIN !」「GetLowestPriceOffersForASIN !」「GetMatchingProductForId !」を設定します。 LabelのIDプロパティを「lblLog2」に書き換えたらForeColorプロパティを追加して「Red」を設定します。 これでメッセージが赤色で表示されます。
    
    ASIN1: <asp:TextBox ID="txtASIN1" runat="server" Text="" Columns="10" />
    ASIN2: <asp:TextBox ID="txtASIN2" runat="server" Text="" Columns="10" />
    ASIN3: <asp:TextBox ID="txtASIN3" runat="server" Text="" Columns="10" />
     
    <asp:Button ID="btnGetPrice1" runat="server"  BackColor="Orange" Text="GetLowestOfferListingsForASIN !" OnClick="btnGetPrice1_Click" />             
    
    <asp:Button ID="btnGetPrice2" runat="server"  BackColor="Orange" Text="GetLowestPriceOffersForASIN !" OnClick="btnGetPrice2_Click" />  
    
    <asp:Button ID="btnGetAttributes" runat="server"  BackColor="Orange" Text="GetMatchingProductForId !" OnClick="btnGetAttributes_Click" />  
      
    <asp:Label ID="lblLog2" runat="server" ForeColor="Red" Text="" />
    
  6. VS2019からWebフォームのコードビハインド(partial class)を表示したら、以下の名前空間(NameSpace)を追加します。
    NOTE: Amazon MWS APIのライブラリーはAmazonのサイトからダウンロードして「AmazonMws」フォルダーに格納しておきます。 最新のライブラリーはAmazonのテクニカルサポートに確認してください。
    
    using MarketplaceWebServiceProducts;
    using MarketplaceWebServiceProducts.Model;
    using System.Text;
    using asp4x.AppLayer;
    
  7. さらに、Webフォームのコードビハインドにクラス「WebConfigArgs」の変数を追加します。WebConfigArgsクラスについては後述します。
    
    public WebConfigArgs wc;
    
  8. WebフォームのPage_Load()イベントに以下のコードを入力します。
    
    protected void Page_Load(object sender, EventArgs e)
    {
        wc = WebConfig.GetWebConfigAmazon();
    
        if (!IsPostBack)
        {
        appName.InnerText = wc.strAppName;                                                  // Home, Liberal Arts,...
        appType.InnerText = String.Format("{0}({1})", wc.strAppType, wc.strAppVersion);     // PROD(V1.0.0) or DEVL(V1.0.0)
        appSub.InnerText = wc.strPoweredBy;                                                 // Powered by ASP.NET 4.x
    
        lblDate.Text = String.Format("{0:yyyy/MM/dd}", DateTime.UtcNow.AddHours(9));
        lblTime.Text = String.Format("{0:HH:mm:ss}", DateTime.UtcNow.AddHours(9));
    
        copyRight.InnerHtml = wc.strCopyRight;                                              // ©2021 Akio Kasai       
        }
    }  
    
  9. Webフォームにコードビハインドに[GetMyPrice]ボタンのクリック時のイベントを追加します。
    
    protected void btnGetMyPrice_Click(object sender, EventArgs e)
    {
        string strSKU1 = txtSKU1.Text.Trim();
        string strSKU2 = txtSKU2.Text.Trim();
        string strSKU3 = txtSKU3.Text.Trim();
    
        // Create a configuration object
        MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
        config.ServiceURL = wc.strServiceUrlJp;
    
        // Create the client itself
        MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(wc.strAppName, wc.strAppVersion, wc.strAccessKeyJp, wc.strSecretKeyJp, config);
    
        SellerSKUListType sellerSKUList = new SellerSKUListType(); // Max 5 SKUs
        sellerSKUList.SellerSKU.Add(strSKU1);
    
        if (!String.IsNullOrEmpty(strSKU2))
        {
        sellerSKUList.SellerSKU.Add(strSKU2);
        }
    
        if (!String.IsNullOrEmpty(strSKU3))
        {
        sellerSKUList.SellerSKU.Add(strSKU3);
        }
    
        // Create a request
        GetMyPriceForSKURequest request = new GetMyPriceForSKURequest(wc.strSellerIdJp, wc.strMarketplaceIdJp, sellerSKUList);
    
        GetMyPriceForSKUResponse response = client.GetMyPriceForSKU(request);
    
        Response.Clear();
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;
        Response.Write(response.ToXML());
        Response.End();
    }
    
  10. 同様の手順で[GetLowestOfferListingsForASIN], [GetLowestPriceOffersForASIN], [GetMatchingProductForId]コマンドのクリック時のイベント処理を追加します。
    
    protected void btnGetPrice1_Click(object sender, EventArgs e)
    {
        string strASIN1 = txtASIN1.Text.Trim();
        string strASIN2 = txtASIN2.Text.Trim();
        string strASIN3 = txtASIN3.Text.Trim();
    
        // Create a configuration object
        MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
        config.ServiceURL = wc.strServiceUrlJp;
    
        // Create the client itself
        MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(wc.strAppName, wc.strAppVersion, wc.strAccessKeyJp, wc.strSecretKeyJp, config);
    
        ASINListType asinList = new ASINListType(); // Max 20 ASINs
        asinList.ASIN.Add(strASIN1);
    
        if (!String.IsNullOrEmpty(strASIN2))
        {
        asinList.ASIN.Add(strASIN2);
        }
    
        if (!String.IsNullOrEmpty(strASIN3))
        {
        asinList.ASIN.Add(strASIN3);
        }
    
        // Create a request
        GetLowestOfferListingsForASINRequest request = new GetLowestOfferListingsForASINRequest(wc.strSellerIdJp, wc.strMarketplaceIdJp, asinList);
        request.ExcludeMe = true;
        request.ItemCondition = "New";  // New,Used,Collectible,Refurbished,Club,Default(All)
    
        GetLowestOfferListingsForASINResponse response = client.GetLowestOfferListingsForASIN(request);
    
        Response.Clear();
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;
        Response.Write(response.ToXML());
        Response.End();
    }
    
    protected void btnGetPrice2_Click(object sender, EventArgs e)
    {
        string strASIN1 = txtASIN1.Text.Trim();
    
    
        // Create a configuration object
        MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
        config.ServiceURL = wc.strServiceUrlJp;
    
        // Create the client itself
        MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(wc.strAppName, wc.strAppVersion, wc.strAccessKeyJp, wc.strSecretKeyJp, config);
    
        string strItemCondition = "New"; // New, Used
    
        // Create a request
        GetLowestPricedOffersForASINRequest request = new GetLowestPricedOffersForASINRequest(wc.strSellerIdJp, wc.strMarketplaceIdJp, strASIN1, strItemCondition);
    
        try
        {
        GetLowestPricedOffersForASINResponse response = client.GetLowestPricedOffersForASIN(request);
    
        Response.Clear();
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;
        Response.Write(response.ToXML());
        Response.End();
        }
        catch (Exception ex)
        {
        lblLog2.Text += String.Format("**ERROR**  client.GetLowestPricedOffersForASIN(): {0}", ex.Message);
        }
    
    }
    
    protected void btnGetAttributes_Click(object sender, EventArgs e)
    {
        string strASIN1 = txtASIN1.Text.Trim();
        string strASIN2 = txtASIN2.Text.Trim();
        string strASIN3 = txtASIN3.Text.Trim();
    
        // Create a configuration object
        MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
        config.ServiceURL = wc.strServiceUrlJp;
    
        // Create the client itself
        MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(wc.strAppName, wc.strAppVersion, wc.strAccessKeyJp, wc.strSecretKeyJp, config);
    
        string strIdType = "ASIN"; // ASIN,GCID,SellerSKU,UPC,EAN,ISBN,JAN       
    
        IdListType idList = new IdListType();  // Max 5 IDs            
        idList.Id.Add(strASIN1);
    
        if (!String.IsNullOrEmpty(strASIN2))
        {
        idList.Id.Add(strASIN2);
        }
    
        if (!String.IsNullOrEmpty(strASIN3))
        {
        idList.Id.Add(strASIN3);
        }
    
        // Create a request
        GetMatchingProductForIdRequest request = new GetMatchingProductForIdRequest(wc.strSellerIdJp, wc.strMarketplaceIdJp, strIdType, idList);
    
        GetMatchingProductForIdResponse response = client.GetMatchingProductForId(request);
    
        Response.Clear();
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;
        Response.Write(response.ToXML());
        Response.End();
    }
    
  11. WebConfigArgsクラス
    
    public class WebConfigArgs
    {
        // Basic info
        public string strAppName { get; set; }              // Home, Liberal Arts, HTML/CSS, Javascript/jQuery, ASP.NET, MS Access, Amazon MWS
        public string strAppType { get; set; }              // PROD(V1.0.0) or DEVL(V1.0.0)
        public string strAppVersion { get; set; }           // V1.0.0
        public string strPoweredBy { get; set; }            // Powered by ASP.NET 4.x
        public string strCopyRight { get; set; }            // ©2021 Akio Kasai
    
    
        // Amazon(0) MWS API info (Current Region)
        public string strServiceUrl { get; set; }                   
        public string strAccessKey { get; set; }                    
        public string strSecretKey { get; set; }                   
        public string strSellerId { get; set; }                     
        public string strAuthToken { get; set; }                   
        public string strMarketplaceId { get; set; }               
        public string strMerchantId { get; set; }                   
    
        // Amazon(1) MWS API info (US)
        public string strServiceUrlUs { get; set; }                 
        public string strAccessKeyUs { get; set; }                  
        public string strSecretKeyUs { get; set; }                
        public string strSellerIdUs { get; set; }                 
        public string strAuthTokenUs { get; set; }                
        public string strMarketplaceIdUs { get; set; }            
        public string strMerchantIdUs { get; set; }              
    
        // Amazon(2) MWS API info (CA)
        public string strServiceUrlCa { get; set; }                
        public string strAccessKeyCa { get; set; }                  
        public string strSecretKeyCa { get; set; }                  
        public string strSellerIdCa { get; set; }                 
        public string strAuthTokenCa { get; set; }                 
        public string strMarketplaceIdCa { get; set; }             
        public string strMerchantIdCa { get; set; }                
    
        // Amazon(3) MWS API info (AU)
        public string strServiceUrlAu { get; set; }               
        public string strAccessKeyAu { get; set; }                
        public string strSecretKeyAu { get; set; }                  
        public string strSellerIdAu { get; set; }                
        public string strAuthTokenAu { get; set; }                 
        public string strMarketplaceIdAu { get; set; }             
        public string strMerchantIdAu { get; set; }                
    
        // Amazon(4) MWS API info (JP)
        public string strServiceUrlJp { get; set; }                
        public string strAccessKeyJp { get; set; }                 
        public string strSecretKeyJp { get; set; }               
        public string strSellerIdJp { get; set; }                  
        public string strAuthTokenJp { get; set; }                
        public string strMarketplaceIdJp { get; set; }             
        public string strMerchantIdJp { get; set; }                
    }
    
  12. GetWebConfigAmazon()メソッド
    
    public static WebConfigArgs GetWebConfigAmazon()
    {
        WebConfigArgs wc = new WebConfigArgs();
    
        // Set Basic info
        wc.strAppName = ConfigurationManager.AppSettings["appName"];          // Home, Liberal Arts,...
        wc.strAppType = ConfigurationManager.AppSettings["appType"];          // PROD or DEVL
        wc.strAppVersion = ConfigurationManager.AppSettings["appVersion"];    // V1.0.0
        wc.strPoweredBy = ConfigurationManager.AppSettings["appPoweredBy"];   // Powered by ASP.NET 4.x
        wc.strCopyRight = ConfigurationManager.AppSettings["appCopyRight"];   // ©2021 Akio Kasai 
    
        // Set Amazon(0) MWS API info (Current Region: JP)
        wc.strAccessKey = ConfigurationManager.AppSettings["accessKey"];
        wc.strSecretKey = ConfigurationManager.AppSettings["secretKey"];
        wc.strServiceUrl = ConfigurationManager.AppSettings["serviceUrl"];
        wc.strSellerId = ConfigurationManager.AppSettings["sellerId"];
        wc.strAuthToken = ConfigurationManager.AppSettings["authToken"];
        wc.strMarketplaceId = ConfigurationManager.AppSettings["marketplaceId"];
        wc.strMerchantId = ConfigurationManager.AppSettings["merchantId"];
    
    
        // Set Amazon(1) MWS API info (US)
        wc.strAccessKeyUs = ConfigurationManager.AppSettings["accessKeyUs"];
        wc.strSecretKeyUs = ConfigurationManager.AppSettings["secretKeyUs"];
        wc.strServiceUrlUs = ConfigurationManager.AppSettings["serviceUrlUs"];
        wc.strSellerIdUs = ConfigurationManager.AppSettings["sellerIdUs"];
        wc.strAuthTokenUs = ConfigurationManager.AppSettings["authTokenUs"];
        wc.strMarketplaceIdUs = ConfigurationManager.AppSettings["marketplaceIdUs"];
        wc.strMerchantIdUs = ConfigurationManager.AppSettings["merchantIdUs"];
    
    
        // Set Amazon(2) MWS API info (CA)
        wc.strAccessKeyCa = ConfigurationManager.AppSettings["accessKeyCa"];
        wc.strSecretKeyCa = ConfigurationManager.AppSettings["secretKeyCa"];
        wc.strServiceUrlCa = ConfigurationManager.AppSettings["serviceUrlCa"];
        wc.strSellerIdCa = ConfigurationManager.AppSettings["sellerIdCa"];
        wc.strAuthTokenCa = ConfigurationManager.AppSettings["authTokenCa"];
        wc.strMarketplaceIdCa = ConfigurationManager.AppSettings["marketplaceIdCa"];
        wc.strMerchantIdCa = ConfigurationManager.AppSettings["merchantIdCa"];
    
    
        // Set Amazon(3) MWS API info (AU)
        wc.strAccessKeyAu = ConfigurationManager.AppSettings["accessKeyAu"];
        wc.strSecretKeyAu = ConfigurationManager.AppSettings["secretKeyAu"];
        wc.strServiceUrlAu = ConfigurationManager.AppSettings["serviceUrlAu"];
        wc.strSellerIdAu = ConfigurationManager.AppSettings["sellerIdAu"];
        wc.strAuthTokenAu = ConfigurationManager.AppSettings["authTokenAu"];
        wc.strMarketplaceIdAu = ConfigurationManager.AppSettings["marketplaceIdAu"];
        wc.strMerchantIdAu = ConfigurationManager.AppSettings["merchantIdAu"];
    
    
        // Set Amazon(4) MWS API info (JP)
        wc.strAccessKeyJp = ConfigurationManager.AppSettings["accessKeyJp"];
        wc.strSecretKeyJp = ConfigurationManager.AppSettings["secretKeyJp"];
        wc.strServiceUrlJp = ConfigurationManager.AppSettings["serviceUrlJp"];
        wc.strSellerIdJp = ConfigurationManager.AppSettings["sellerIdJp"];
        wc.strAuthTokenJp = ConfigurationManager.AppSettings["authTokenJp"];
        wc.strMarketplaceIdJp = ConfigurationManager.AppSettings["marketplaceIdJp"];
        wc.strMerchantIdJp = ConfigurationManager.AppSettings["merchantIdJp"];
    
        return (wc);
    
    
  13. Web.config(1) : Amazonフォルダに登録されているWeb.configの内容
    
    <appSettings>
    <!--Basic info-->
    <add key="appName" value="Amazon MWS" />		
    
    <!--Amazon(0) MWS API info (Current Region : JP)-->
    <add key="accessKey" value="●●●" />
    <add key="secretKey" value="●●●" />
    <add key="serviceUrl" value="●●●" />
    <add key="sellerId" value="●●●" />
    <add key="marketplaceId" value="●●●" />
    <add key="merchantId" value="●●●" />
    <add key="mwsDeveloperId" value="●●●" />		
    		
    		
    <!--Amazon(1) MWS API info (US)-->
    <add key="accessKeyUs" value="●●●" />
    <add key="secretKeyUs" value="●●●" />
    <add key="serviceUrlUs" value="●●●" />
    <add key="sellerIdUs" value="●●●" />
    <add key="marketplaceIdUs" value="●●●" />
    <add key="merchantIdUs" value="●●●" />
    <add key="mwsDeveloperIdUs" value="●●●" />
    
    <!--Amazon(2) MWS API info (CA)-->
    <add key="accessKeyCa" value="●●●" />
    <add key="secretKeyCa" value="●●●" />
    <add key="serviceUrlCa" value="●●●" />
    <add key="sellerIdCa" value="●●●" />
    <add key="marketplaceIdCa" value="●●●" />
    <add key="merchantIdCa" value="●●●" />
    <add key="mwsDeveloperIdCa" value="●●●" />
    
    <!--Amazon(3) MWS API info (AU)-->
    <add key="accessKeyAu" value="●●●" />
    <add key="secretKeyAu" value="●●●" />
    <add key="serviceUrlAu" value="●●●" />
    <add key="sellerIdAu" value="●●●" />
    <add key="marketplaceIdAu" value="●●●" />
    <add key="merchantIdAu" value="●●●" />
    <add key="mwsDeveloperIdAu" value="●●●" />
    
    <!--Amazon(4) MWS API info (JP)-->
    <add key="accessKeyJp" value="●●●" />
    <add key="secretKeyJp" value="●●●" />
    <add key="serviceUrlJp" value="●●●" />
    <add key="sellerIdJp" value="●●●" />
    <add key="marketplaceIdJp" value="●●●" />
    <add key="merchantIdJp" value="●●●" />
    <add key="mwsDeveloperIdJp" value="●●●" />
    		
    </appSettings>
    
  14. Web.config(2) : Rootフォルダに登録されているWeb.configの内容
    NOTE:CopyRightの「©」はエスケースシーケンス「&copy;」を使用しています。 Web.configファイルはXML形式なので「&」を「&amp;」のように定義します。
    
    <appSettings>
    <!--Basic info-->
    <add key="appName" value="Home" />				
    <add key="appType" value="DEVL" />				
    <add key="appVersion" value="V1.0.0" />
    <add key="appPoweredBy" value="Powered by ASP.NET 4.x" />
    <add key="appCopyRight" value="&amp;copy;2021 Akio Kasai" />	
    </appSettings>
    

操作手順

  1. まずは実行するAPIを決めます。
  2. GetMyPriceForSKU() APIを実行するときは、テキストボックスにAmazon.co.jpに登録されている商品のSKUを1~3個入力します。
  3. そして、[GetMyPriceForSKU]ボタンをクリックします。
  4. GetMyPriceForSKU()APIのレスポンスがブラウザにXML形式で表示されます。
  5. GetLowestOfferListingsForASIN(), GetLowestPriceOffersForASIN(), GetMatchingProductForId() APIを実行するときは、 テキストボックスにAmazon.co.jpに登録されている商品のASINを1~3個入力します。
  6. そして、実行したいAPIのボタンをクリックします。
  7. APIのレスポンスがブラウザにXML形式で表示されます。

Live DEMO

GetMyPriceForSKU(jp)
SKU1:
SKU2:
SKU3:




GetLowestOfferListingsForASIN(jp), GetLowestPriceOffersForASIN(jp), GetMatchingProductForId(jp)
ASIN1:
ASIN2:
ASIN3:








Amazon MWS PV1.0.0 ©2024 Akio Kasai
Powered by ASP.NET 4.x