如何使用亚马逊MWS API下载亚马逊报告

问题描述 投票:0回答:1

我正在使用此链接中的Amazon Reports API客户端库 - C# - 版本2009-01-01:Amazon Reports Client C#

问题是即使遵循该库中的样本,MarketplaceWebServiceClient.cs类中的GetReport()方法也不会在GetReportResponse()响应中返回真实的Report。

似乎发生了一些事情,GetReportResponse返回NULL而不是任何数据。

c# amazon-mws
1个回答
0
投票

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

所以,我从这个网站得到了正确答案: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);
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.