jlevers / selling-partner-api

A PHP client library for Amazon's Selling Partner API
BSD 3-Clause "New" or "Revised" License
410 stars 200 forks source link

Calling getReportDocument() Returns 400 Error with Invalid Report Document ID #671

Closed sydy closed 7 months ago

sydy commented 8 months ago

Problem description:

When calling the getReportDocument() method, correct $documentId and $reportType parameters were used, but an incorrect response was received.

Error:

Bad Request (400) Response: {
  "errors": [
    {
      "code": "InvalidInput",
      "message": "Invalid or unsupported document ID found",
      "details": ""
    }
  ]
}

Code

$connector = SellingPartnerApi::make(/* ... */)->seller();
$response = $connector->reports()->getReportDocument($documentId, $reportType);
jlevers commented 8 months ago

Could you please provide your full code? Without more context, it's hard for me to figure out where the issue might be. I think this is relatively unlikely to be an issue with this SDK, but it's certainly possible.

sydy commented 7 months ago

Hello, thank you for your response. Please provide the following details: Framework: Laravel 10.10 Report Type: GET_MERCHANT_LISTINGS_ALL_DATA

Run the code.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use SellingPartnerApi\SellingPartnerApi;
use SellingPartnerApi\Enums\Endpoint;

class Amazon extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'app:amazon';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Execute the console command.
     */
    public function handle()
    {

        $connector = SellingPartnerApi::make(
            clientId: config('services.amazon.client_id'),
            clientSecret: config('services.amazon.client_secret'),
            refreshToken: config('services.amazon.refresh_token'),
            endpoint: Endpoint::NA,
        )->seller();

        $response = $connector->reports()->getReport(64570019820);
        $data = $response->json();
        print_r($data);

        $response = $connector->reports()->getReportDocument(
            $data['reportDocumentId'],
            $data['reportType']
        );

        print_r($response->json());

    }
}

Response


   Saloon\Exceptions\Request\ClientException 

  Bad Request (400) Response: {
  "errors": [
    {
      "code": "InvalidInput",
      "message": "Invalid or unsupported document ID found",
      "details": ""
    }
  ]
}

  at vendor/saloonphp/saloon/src/Helpers/RequestExceptionHelper.php:51
     47▕             $response->clientError() => ClientException::class,
     48▕             default => RequestException::class,
     49▕         };
     50▕ 
  ➜  51▕         return new $requestException($response, null, 0, $previous);
     52▕     }
     53▕ }
     54▕ 

      +36 vendor frames 

  37  app/Console/Commands/Amazon.php:45
      SellingPartnerApi\Seller\ReportsV20210630\Api::getReportDocument()
      +12 vendor frames 

  50  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
jbaelo commented 7 months ago

Same issue here trying to get report document for report type "GET_FLAT_FILE_OPEN_LISTINGS_DATA".

When creating GetReportDocument this adds RestrictedDataToken on every report, no matter type.

https://github.com/jlevers/selling-partner-api/blob/804166c90434f3c3567bf180168acdbcc9992412/src/Seller/ReportsV20210630/Requests/GetReportDocument.php#L29

But according to amazon official docs only some reports are restricted:

RestrictedDataToken

jlevers commented 7 months ago

@jbaelo thanks for the PR! Would you mind giving me push permissions to your PR branch (instructions here)? Because the GetReportDocument class is autogenerated, we need to go about this a slightly different way.

jlevers commented 7 months ago

(Whoops, meant to comment that on the PR itself, not this issue)

misterakko commented 7 months ago

Excuse me if I intrude, but why does $connector->reports()->getReportDocument() request and require a second parameter? Amazon documentation is very clear, the APIs only accept a single parameter, the reportDocumentId https://developer-docs.amazon.com/sp-api/docs/reports-api-v2021-06-30-reference#getreportdocument

jlevers commented 7 months ago

It's to enable the document helper functionality that I've built into this library (see the DownloadsDocument and UploadsDocument traits). You're right that it's a departure from the Amazon spec, but I think it's worth it since it enables quite a lot of extra functionality and is very little additional work for end users.

j-masci commented 7 months ago

I'm getting the same issue with GET_MERCHANT_LISTINGS_DATA. I'm pretty sure I passed all data correctly. I can see in a reports.json file, this report type isn't restricted.

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: POST https://sellingpartnerapi-na.amazon.com/tokens/2021-03-01/restrictedDataToken resulted in a 400 Bad Request response: { "errors": [ { "code": "InvalidInput", "message": "Invalid or unsupported document ID found", " (truncated...)

Edit: Ok I see the open PR will address the issue. I'll wait for it to get merged.

jlevers commented 7 months ago

@j-masci this is because of an issue with how the library is generating RDTs – once I'm able to push to the fork branch on PR #682 (cc @jbaelo), I'll release the fix. If that doesn't happen in the next day or so I'll just commit it all directly, but I like to go through people's PRs whenever possible so that they get credit as a contributor.

jbaelo commented 7 months ago

@jlevers "Allow edits and access to secrets by maintainers" option not showing in PR, anyway we give you write permission to our repo

jlevers commented 7 months ago

This should be fixed in v6.0.5.