如何使用Amazon MWS API下载Amazon报告

时间:2018-09-07 02:44:27

标签: c# amazon-mws

我正在通过以下链接使用Amazon Reports API客户端库-C#-版本2009-01-01:Amazon Reports Client C#

问题在于,即使遵循该库中的示例,MarketplaceWebServiceClient.cs类的GetReport()方法也不会在GetReportResponse()响应中返回真实的报告。

GetReportResponse返回NULL而不包含任何数据的地方似乎发生了什么。

1 个答案:

答案 0 :(得分:0)

好吧,经过一个小时的谷歌搜索,我想我找到了这个问题的答案。 如果您遵循它们,Amazon库样本将永远不会获得正确的结果。

因此,我从此站点获得了正确答案:download-amazon-reports-using-mws-api

基本上,最重要的行是这些行:

var request = new GetReportRequest();

//... (more details in a few)

var path = request.ReportId + "_" + Guid.NewGuid();
var thePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + string.Format("{0}.txt", path);

request.Report = File.Open(thePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);  <-- this line makes the trick!

您需要打开文件并在请求报告时传递参考。 返回的报告文件将位于thePath

这是我创建的一种测试方法,可用于检索报告。相应地更新您的MWS API凭据。您还需要从上面的链接下载Amazon Reports API库C#。

        [TestMethod]
        public void TestGetReport()
        {
            // Developer AWS access key
            var accessKey = "[YOUR-ACCESS-KEY]";

            // Developer AWS secret key
            var secretKey = "[YOUR-SECRET-KEY]";

            // The client application name
            var appName = "MWS Reports API SAMPLE";

            // The client application version
            var appVersion = "1.0";

            // The endpoint for region service and version (see developer guide)
            // ex: https://mws.amazonservices.com
            var serviceURL = "https://mws.amazonservices.com";

            var config = new MarketplaceWebServiceConfig();
            config.ServiceURL = serviceURL;

            var client = new MarketplaceWebServiceClient(accessKey, secretKey, appName, appVersion, config);

            var request = new GetReportRequest();
            var sellerId = "[YOUR-SELLER-ID]";
            request.Merchant = sellerId;
            var mwsAuthToken = "[YOUR-MWS-AUTH-TOKEN]";
            request.MWSAuthToken = mwsAuthToken;
            request.ReportId = "[YOUR-REPORT-ID]";

            var path = request.ReportId + "_" + Guid.NewGuid();
            var thePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + string.Format("{0}.txt", path);

            request.Report = File.Open(thePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            //request.ReportTypeList = new TypeList { Type = new List<string> { "_GET_V2_SETTLEMENT_REPORT_DATA_XML_" } };

            try
            {
                GetReportResponse response = null;
                response = client.GetReport(request);
                Console.WriteLine("Response:");
                var rhmd = response.ResponseHeaderMetadata;
                // We recommend logging the request id and timestamp of every call.
                Console.WriteLine("RequestId: " + rhmd.RequestId);
                Console.WriteLine("Timestamp: " + rhmd.Timestamp);
                var responseXml = response.ToXML();
                Console.WriteLine(responseXml);
                request.Report.Close();
            }
            catch (MarketplaceWebServiceException ex)
            {
                // Exception properties are important for diagnostics.
                ResponseHeaderMetadata rhmd = ex.ResponseHeaderMetadata;
                Console.WriteLine("Service Exception:");
                if (rhmd != null)
                {
                    Console.WriteLine("RequestId: " + rhmd.RequestId);
                    Console.WriteLine("Timestamp: " + rhmd.Timestamp);
                }

                Console.WriteLine("Message: " + ex.Message);
                Console.WriteLine("StatusCode: " + ex.StatusCode);
                Console.WriteLine("ErrorCode: " + ex.ErrorCode);
                Console.WriteLine("ErrorType: " + ex.ErrorType);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Message: " + ex.Message);
            }
        }