Issue with Custom Guzzle Client in authenticationClient for SellingPartnerApi::seller() method in PHP SDK for Amazon

Updated: Feb 01, 2025

Issue with Custom Guzzle Client in authenticationClient for SellingPartnerApi::seller() method in PHP SDK for Amazon

To address the issue with a custom Guzzle client in authenticationClient for the seller() method in the PHP SDK for Amazon, you need to ensure that the client is properly configured for OAuth authentication. Here's a step-by-step guide to help you set up your custom Guzzle client for the Amazon Selling Partner API.

  1. Install the required dependencies: First, make sure you have the Amazon AWS SDK for PHP and Guzzle installed in your project. You can install them using Composer:
composer require aws/aws-sdk-php
composer require guzzlehttp/guzzle
  1. Create a custom Guzzle client: Create a new PHP file, e.g., CustomGuzzleClient.php, and write the following code to create a custom Guzzle client:
<?php

namespace App\Services;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;

class CustomGuzzleClient
{
    protected $client;

    public function __construct(array $config = [])
    {
        $this->client = new GuzzleClient($config);
    }

    public function sendRequest(Request $request): Response
    {
        try {
            return $this->client->send($request);
        } catch (RequestException $e) {
            throw new \Exception('Error sending request: ' . $e->getMessage(), $e->getCode());
        }
    }
}
  1. Configure the client for OAuth authentication: Update the CustomGuzzleClient.php file to include the OAuth authentication configuration:
<?php

namespace App\Services;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Amazon\SellingPartner\Api\Auth\AuthenticationClient;
use Amazon\SellingPartner\Api\SellingPartnerApi;

class CustomGuzzleClient
{
    protected $client;
    protected $authClient;

    public function __construct(array $config = [])
    {
        $this->client = new GuzzleClient([
            'base_uri' => 'https://sellingpartnerapi-na.amazon.com/',
            'headers' => [
                'Content-Type' => 'application/json',
            ],
        ]);

        $this->authClient = new AuthenticationClient([
            'clientId' => 'YOUR_CLIENT_ID',
            'clientSecret' => 'YOUR_CLIENT_SECRET',
            'region' => 'US',
            'accessKeyId' => 'YOUR_ACCESS_KEY_ID',
            'secretAccessKey' => 'YOUR_SECRET_ACCESS_KEY',
            'refreshToken' => 'YOUR_REFRESH_TOKEN',
        ]);
    }

    public function sendRequest(Request $request): Response
    {
        try {
            $accessToken = $this->authClient->getAccessToken();
            $headers = [
                'Authorization' => 'Bearer ' . $accessToken['accessToken'],
            ];

            $request = $request->withHeaders($headers);

            return $this->client->send($request);
        } catch (RequestException $e) {
            throw new \Exception('Error sending request: ' . $e->getMessage(), $e->getCode());
        }
    }
}

Replace YOUR_CLIENT_ID, YOUR_CLIENT_SECRET, YOUR_ACCESS_KEY_ID, YOUR_SECRET_ACCESS_KEY, and YOUR_REFRESH_TOKEN with your actual credentials.

  1. Use the custom Guzzle client in the seller() method: Finally, update the code that initializes and uses the SellingPartnerApi instance to use your custom Guzzle client:
<?php

use Amazon\SellingPartner\Api\SellingPartnerApi;
use App\Services\CustomGuzzleClient;

$config = [
    'version' => '2018-09-01',
];

$customGuzzleClient = new CustomGuzzleClient($config);

$sellingPartnerApi = new SellingPartnerApi([
    'region' => 'US',
    'auth' => [
        'client' => $customGuzzleClient,
    ],
]);

$seller = $sellingPartnerApi->seller()->get('your-seller-id');
print_r($seller->toArray());

Replace your-seller-id with the actual seller ID you want to fetch data for.

With these changes, your custom Guzzle client should now be properly configured for OAuth authentication and work with the seller() method in the Amazon Selling Partner API PHP SDK.