Search code examples
phpcomposer-phpgoogle-api-php-client

Google APIClient Class not found using Composer


I´m using composer to install the the google api client Packagist Google API Client.

After I installed it using composer require google/apiclient it installed like normal. After that I tried to use it in the example below

<?php
use Google\Service\ShoppingContent;
use Google\Auth\Credentials\ServiceAccountCredentials;

/**
 *
 */
class GoogleShoppingContentAPI
{
  private $service;

  public function __construct($credentialsPath) {
    // Create the credentials object
    $credentials = new ServiceAccountCredentials(
        ShoppingContent::CONTENT,
        $credentialsPath
    );
    // Create the Shopping Content Service with the credentials
    $this->service = new ShoppingContent(['credentials' => $credentials]);
  }

  public function getProducts($merchantID)
  {
    try {
      // Fetch a list of products
      $products = $this->service->products->listProducts($merchantID);
      // Return the product data as an array
      return $products->getResources();
    } catch (Google\Service\Exception $e) {
      throw new Exception("Error: " . $e->getMessage());
    }
  }
}

However when running the code it prints out this error:

Class Fatal error: Uncaught Error: Class "Google\Auth\Credentials\ServiceAccountCredentials" not found in /Users/Shared/www/inc/google.inc.php on line 15

It is in our autoload and our autoload is required before running the code aboved. The Google namespace isnt occupied by another package aswell.

I already looked through our project and did not found any duplicate namespaces related to google. Also reinstalling aswell composer update did not changed anything.


Solution

  • Okay, I´m a bit embarrassed to admit but I ran the composer require command in the wrong folder. So that's why it got installed but it wasn't able to be autoloaded. Shame on me. 😅