首页 > 解决方案 > 我无法在 Amazon Selling Partner API 的报告中显示我的库存

问题描述

object(ClouSale\AmazonSellingPartnerAPI\Models\Reports\GetReportResponse)#482 (1) { ["container":protected]=> array(2) { ["payload"]=> object(ClouSale\AmazonSellingPartnerAPI\Models\Reports\Report)#491 (1) { ["container":protected]=> array(11) { ["marketplace_ids"]=> array(1) { [0]=> string(14) "A1VC38T7YXB528" } ["report_id"]=> string(11) "55933018913" ["report_type"]=> string(30) "GET_MERCHANT_LISTINGS_ALL_DATA" ["data_start_time"]=> object(DateTime)#506 (3) { ["date"]=> string(26) "2021-10-13 04:42:10.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["data_end_time"]=> object(DateTime)#483 (3) { ["date"]=> string(26) "2021-10-13 04:42:10.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["report_schedule_id"]=> NULL ["created_time"]=> object(DateTime)#502 (3) { ["date"]=> string(26) "2021-10-13 04:42:10.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["processing_status"]=> string(4) "DONE" ["processing_start_time"]=> object(DateTime)#481 (3) { ["date"]=> string(26) "2021-10-13 04:42:16.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["processing_end_time"]=> object(DateTime)#480 (3) { ["date"]=> string(26) "2021-10-13 04:42:25.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["report_document_id"]=> string(73) "amzn1.spdoc.1.3.2149a182-4354-4177-b03e-0bc4552cb190.T25GAW47NVJ50Z.47700" } } ["errors"]=> NULL } }

在此处输入图像描述

我想使用 Amazon Selling Partner API 显示我的库存。首先,我为此创建了一份报告。我有报告 ID、文档 ID 等。当我使用 GetReports 函数使用此信息提取报告时,不会出现库存。正常吗?

我的期望是 在此处输入图像描述

标签: amazonamazon-mwsselling-partner-api

解决方案


我解决了我的问题。

首先,如果您想查看第二张图片(我的期望),您必须需要文档 ID。

所以

  1. 创建报告并获取报告 ID
  2. 使用 reportID 获取 documentID
  3. 使用 getReportDocument 函数和 documentID
  4. 打开解密文档并读取文件

这是我的代码(在 laravel 上)

 public function getInventoryReportData() {
        header('Content-Type: text/html; charset=utf-8');

        $options = [
            'refresh_token' => self::constOptions["amazon"]["refresh_token"],
            'client_id' => self::constOptions["amazon"]["client_id"],
            'client_secret' => self::constOptions["amazon"]["client_secret"],
            'region' => \ClouSale\AmazonSellingPartnerAPI\SellingPartnerRegion::$FAR_EAST,
            'access_key' => self::constOptions["amazon"]["access_key"],
            'secret_key' => self::constOptions["amazon"]["secret_key"],
            'endpoint' => \ClouSale\AmazonSellingPartnerAPI\SellingPartnerEndpoint::$FAR_EAST,
            'role_arn' => self::constOptions["amazon"]["role_arn"],
        ];
        $accessToken = \ClouSale\AmazonSellingPartnerAPI\SellingPartnerOAuth::getAccessTokenFromRefreshToken(
            $options['refresh_token'],
            $options['client_id'],
            $options['client_secret']
        );
        $config = \ClouSale\AmazonSellingPartnerAPI\Configuration::getDefaultConfiguration();
        $config->setHost($options['endpoint']);
        $config->setAccessToken($accessToken);
        $config->setAccessKey($options['access_key']);
        $config->setSecretKey($options['secret_key']);
        $config->setRegion($options['region']);
        $apiInstance = new \ClouSale\AmazonSellingPartnerAPI\Api\ReportsApi($config);


        $report_document_id = "YOUR_DOCUMENT_ID_IN_HERE"; 

        try {
            $result = $apiInstance->getReportDocument($report_document_id);

            $key = base64_decode($result->getPayload()->getEncryptionDetails()->getKey());
            $iv = base64_decode($result->getPayload()->getEncryptionDetails()->getInitializationVector());
            $decryptedData = openssl_decrypt(file_get_contents($result->getPayload()->getUrl()), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);

            $convertEncodeData =  mb_convert_encoding($decryptedData, 'UTF-8', 'ASCII, JIS, UTF-8, SJIS');

            echo $convertEncodeData;

        } catch (Exception $e) {
            echo 'Exception when calling ReportsApi->getReportDocument: ', $e->getMessage(), PHP_EOL;
        }
    }

推荐阅读