• Amzon MWS API开发之 上传数据


    Amzon MWS API开发之 上传数据

     

    亚马逊上传数据,现有能操作的功能有很多:库存数量、跟踪号、价格、商品.......

    我们可以设置FeedType值,根据需要,再上传对应的xml文件即可。

    下面可以看看FeedType类型

    这次我们拿同步价格为例子,首先我们来熟悉一下Amazon MWS 提供的上传接口实现流程。

    详细流程可访问:http://docs.developer.amazonservices.com/zh_CN/feeds/Feeds_Overview.html

    上传流程:

    在此,简要说明一下大致的步骤和流程:

    第一步:建立请求

        通过MWS提供的XML程序开发指南上,根据需求,找到对应的XSD和XML实例。XML程序开发指南下载地址:点击下载

        通过程序对XML进行拼接,生存一个XML文件,保存在本地。

        调用MWS客户端的SubmitFeed方法建立一个请求,设置FeedContent值为我们拼接生存的XML文件的流。

        在建立请求后,亚马逊接受到请求后,会返回一个FeedSubmissionId值。

    第二步:上传数据

        调用GetFeedSubmissionList接口方法,将第一步操作返回的FeedSubmissionId值,设置到请求参数FeedSubmissionIdList中。

          此刻,获得Amazon的返回结果,我们可以通过FeedProcessingStatusList状态来判断数据是否上传完成。

               当状态为" _DONE_" 时,说明已经上传成功,接着执行后续操作了。

        当状态为" _IN_PROGRESS_" ,此刻正在上次数据,如果数据量大的情况下,我建议大家Sleep 一会,个人建议Sleep时间设置为1—5分钟之间,视个人情况而定。

    第三步:接受上传结果

              在第二步的上传状态返回" _DONE_"之后,我们可以调用GetFeedSubmissionResult方法,设置第一步返回的FeedSubmissionId参数,来获得上传结果信息。

              上传结果信息包含成功个数,失败的具体信息等。通过核对失败的信息,我们修改后可以继续上传。

               这就是整个的流程,没以生硬的MWS文档来讲解,希望大家能够理解这么一个流程。

     实例DEMO:

    复制代码
      1    /// <summary>
      2     /// 上传数据客户端
      3     /// </summary>
      4     public class FeedClient
      5     {
      6 
      7         private FeedClient() { }
      8 
      9         public FeedClient(string feedType)
     10         {
     11             this.FeedType = feedType;
     12         }
     13 
     14         /// <summary>
     15         /// 上传类型
     16         /// </summary>
     17         string FeedType { get; set; }
     18 
     19         /// <summary>
     20         /// 获得账户信息
     21         /// </summary>
     22         Account Account { get; set; }
     23 
     24         private MarketplaceWebServiceConfig GetConfig()
     25         {
     26             var config = new MarketplaceWebServiceConfig();
     27             config.ServiceURL = Account.ServiceUrl;
     28             return config;
     29         }
     30 
     31         private MarketplaceWebServiceClient GetClient()
     32         {
     33             var config = this.GetConfig();
     34             var client = new MarketplaceWebServiceClient(Account.AppName,
     35               Account.AppVersion, Account.AccessKeyId, Account.SecretAccessKey, config);
     36             return client;
     37         }
     38 
     39         /// <summary>
     40         /// Step 1:  提交XML或txt 上传文件,亚马逊服务端接受到数据,返回一个FeedSubmissionId
     41         /// </summary>
     42         /// <returns></returns>
     43         public string SubmitFeed()
     44         {
     45             var client = GetClient();
     46             var request = new SubmitFeedRequest();
     47             request.FeedType = this.FeedType;       //!上传商品数据
     48             request.MarketplaceIdList = new IdList();
     49             request.MarketplaceIdList.Id = new List<string> { Account.MarketplaceId };
     50 
     51             request.Merchant = Account.MerchantId;
     52             string filePath = @"D:HUAGE.txt"; //PathHelper.CreateFile(Account.AppName, "FeedContent");
     53             request.FeedContent = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
     54             request.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(request.FeedContent);
     55             request.FeedContent.Position = 0;
     56 
     57             var response = client.SubmitFeed(request);
     58             var result = response.SubmitFeedResult;
     59             return result.FeedSubmissionInfo.FeedSubmissionId;
     60         }
     61 
     62         /// <summary>
     63         /// Step 2: 提交一个SubmissionList,等待亚马逊返回"_DONE"状态,如果没有返回则一直等待。
     64         /// </summary>
     65         /// <param name="feedSubmissionId">feedSubmissionId</param>
     66         /// <returns></returns>
     67         public bool GetFeedSubmissionList(string feedSubmissionId)
     68         {
     69             bool isSuccess = true;
     70             var client = GetClient();
     71             var request = new GetFeedSubmissionListRequest();
     72             request.FeedSubmissionIdList = new IdList();
     73             request.FeedSubmissionIdList.Id = new List<string> { feedSubmissionId };
     74 
     75             while (isSuccess)
     76             {
     77                 var response = client.GetFeedSubmissionList(request);
     78                 var result = response.GetFeedSubmissionListResult;
     79 
     80                 foreach (var item in result.FeedSubmissionInfo)
     81                 {
     82                     if (item.FeedProcessingStatus == "_Done")
     83                     {
     84                         isSuccess = false;
     85                     }
     86                     else
     87                     {
     88                         System.Threading.Thread.Sleep(2 * 60 * 1000);   //! 休息一会。 
     89                     }
     90                 }
     91             }
     92             return isSuccess;
     93         }
     94 
     95         /// <summary>
     96         ///  Step 3: 获得上传结果,如果没有错,亚马逊服务端返回处理报告,否则返回错误的上传数据内容。
     97         /// </summary>
     98         /// <param name="feedSubmissionId">feedSubmissionId</param>
     99         /// <returns></returns>
    100         public bool GetFeedSubmissionResult(string feedSubmissionId)
    101         {
    102             var client = GetClient();
    103             var request = new GetFeedSubmissionResultRequest();
    104             request.FeedSubmissionId = feedSubmissionId;
    105             string filePath = PathHelper.CreateFile(Account.AppName, "FeedResult");
    106             request.FeedSubmissionResult = File.Open(filePath, FileMode.Open, FileAccess.Read);
    107             request.Merchant = Account.MerchantId;
    108 
    109             var response = client.GetFeedSubmissionResult(request);
    110             if (response.IsSetGetFeedSubmissionResultResult())
    111             {
    112                 var result = response.GetFeedSubmissionResultResult;
    113                 if (result.IsSetContentMD5())
    114                 {
    115                     return true;
    116                 }
    117             }
    118             return false;
    119         }
    120 
    121         /// <summary>
    122         /// 整合上传数据功能
    123         /// </summary>
    124         public bool SubmitFile()
    125         {
    126             var feedSubmissionId = SubmitFeed();
    127             if (!string.IsNullOrEmpty(feedSubmissionId))
    128             {
    129                 if (GetFeedSubmissionList(feedSubmissionId))
    130                 {
    131                     return GetFeedSubmissionResult(feedSubmissionId);
    132                 }
    133             }
    134             return false;
    135         }
    136     }
    复制代码

    错误消息解决方案汇总:

          在上传过程中,经常会出现调用接口出现的异常,我将结合在工作中出现的异常实例。整理放出来,提供解决方案。

    ===============================================================================================================

    Amazon Feeds API C# Integration

    I’m in the process of building an application which requires strong integration with the Amazon API. I needed to submit product feeds, update inventory and prices, as well as downloading reports and couple other things. When I first was given the project, of course the first I did was a quick google search to see what kind of documentation is out there on the API. There was some info out there (links below), but I had to do a lot playing around first with bits of google searches to get what I needed.

    In hopes of making this easier for other Amazon API developers, I’ll outline the basic process for a feed submission and some methods that I’ve ended up integrating into my application using the Amazon Feeds API.

    Here’s a few FeedTypes I’ve used.

    • _POST_INVENTORY_AVAILABILITY_DATA_ – update inventory.
    • _POST_PRODUCT_PRICING_DATA_ – update pricing.
    • _POST_PRODUCT_DATA_ – add products to listing.
    • _POST_PRODUCT_OVERRIDES_DATA_ – override info, such as shipping costs.

    To get started, you’ll need your Amazon MarketPlaceId, Merchant, Access Key and Secret Access Key, and of course the actual Feeds API.

    Note, in the examples posted below, there’s a method called GetClient() which is used. It returns a MarketplaceWebserviceClient object. Looks like this:

    private MarketplaceWebServiceClient GetClient()
    {
        MarketplaceWebServiceConfig mwsConfig = new MarketplaceWebServiceConfig();
        mwsConfig.ServiceURL = "https://mws.amazonservices.com";
        mwsConfig.SetUserAgentHeader("YourCompany", "1.0", "C#", new string[] { });
    
        return new MarketplaceWebServiceClient(AccessKey, SecretAccessKey, mwsConfig);
    }

    I’ve created a generic method to submit all feeds, which goes like this:

    public SubmitFeedResult SubmitFeed(FileInfo file, FeedType feedType)
    {
        SubmitFeedResponse response = new SubmitFeedResponse();
    
        SubmitFeedRequest sfrequest = new SubmitFeedRequest();
        sfrequest.Merchant = Merchant;
        sfrequest.MarketplaceIdList = MarketPlaceID;
    
        using (FileStream stream = new FileStream(file.FullName, FileMode.Open, FileAccess.ReadWrite))
        {
            sfrequest.FeedContent = stream;
            sfrequest.ContentMD5 = MarketplaceWebServiceClient.CalculateContentMD5(sfrequest.FeedContent);
            sfrequest.FeedContent.Position = 0;
            sfrequest.FeedType = feedType.ToString();
    
            response = GetClient().SubmitFeed(sfrequest);
        }
    
        return response.SubmitFeedResult;
    }

    I pass in the file which contains the XML to submit, along with an FeedType enum I’ve declared, which contains my FeedTypes listed above.

    Once you submit a feed, you’d want to see the status. Did it go through, were there any errors/warnings etc.

    There’s a FeedSubmissionInfo object that returns info for a specific submission. I use this method to return a list of feed submissions.

    public List<FeedSubmissionInfo> GetFeedSubmissionList()
    {
        GetFeedSubmissionListRequest request = new GetFeedSubmissionListRequest();
        request.Merchant = Merchant;
        GetFeedSubmissionListResponse response = GetClient().GetFeedSubmissionList(request);
        return response.GetFeedSubmissionListResult.FeedSubmissionInfo;
    }

    You can now get one FeedSubmissionID from one of the FeedSubmissionInfo objects returned, and pass it in to the following method, which will create and return a FileInfo containing the response. You can further modify this to parse out the XML and reformat the text so it’s readable. Change the path to your preferred location for the file to be created to.

    public FileInfo GetFeedSubmissionResultFile(string feedSubmissionID)
    {
        GetFeedSubmissionResultRequest request = new GetFeedSubmissionResultRequest();
        request.FeedSubmissionId = feedSubmissionID;
        request.Merchant = Merchant;
    
        GetFeedSubmissionResultResponse response;
    
        string path = HttpContext.Current.Server.MapPath("~/Files/SubmissionResult-" + feedSubmissionID + ".txt);
    
        using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
        {
            request.FeedSubmissionResult = stream;
            response = GetClient().GetFeedSubmissionResult(request);
        }
    
        return new FileInfo(path);
    }

    That’s the basic process of a feed submission using the Amazon Feed API. You can do plenty more with  the Amazon APIs, like managing orders, prices, inventory, and shipping.

  • 相关阅读:
    从视频中每隔固定帧进行提取图片
    np.concatenate的超简单理解
    python-OOP(面向对象)
    机器学习中的ground truth
    深度学习网络中backbone是什么意思?
    缓存
    Linux基础命令
    openoffice相关命令
    HTTP协议
    Solr基础
  • 原文地址:https://www.cnblogs.com/qq260250932/p/5456803.html
Copyright © 2020-2023  润新知