0

I use OAuth 2.0 flow + google-api-php-client + Mybusiness PHP Classes, but I got errors 'class not found', see below:

use App\Vendors\Google_Service_MyBusiness as MyBusiness;
....

$gClient->setAccessToken($access_token);
$oauth2 = new \Google_Service_Oauth2($gClient);
$userInfo = $oauth2->userinfo->get();
print_r($userInfo); //Works!

$mybusinessService = new MyBusiness($gClient);

//return Class 'Google_Service_MyBusiness_Location' not found:
$mybusinessService->accounts->get('accounts/xxxxxxxx');

//return 'Google_Service_MyBusiness_ListAccountsResponse' not found:
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();

Full error:

Class 'Google_Service_MyBusiness_Account' not found in file /vendor/google/apiclient/src/Google/Http/REST.php on line 128

Class MyBusiness added in autoload:

"autoload" : {
    "files": [
        "app/Vendors/MyBusiness.php"
    ]
}

All required clsses (eg. Google_Service_MyBusiness_ListAccountsResponse, Google_Service_MyBusiness_Location are in app/Vendors/MyBusiness.php)

What is wrong with my code?

pelcomppl
  • 575
  • 2
  • 6
  • 16
  • Are you using composer to set this up? Does other autoloading work? I'm not sure from the question where exactly this code is, is it in some class called by something else? If it's in a separate file I would expect to see some bootstrap code to include the autoload file, like [this line](https://github.com/laravel/laravel/blob/master/public/index.php#L24) in the laravel index file – mickadoo Nov 26 '18 at 09:23
  • @mickadoo I use composer. The code is in controller. Other autoloading works. File `app/Vendors/MyBusiness.php` has many classes: Class `Google_Service_MyBusiness` is visible in controller and works, but other classes from this file use in `vendor/google/apiclient/src/Google/Http/REST.php` not. Is it full answer for you? – pelcomppl Nov 26 '18 at 09:31
  • Have you tried manually requiring the file that contains that class? If that works then it should narrow it down to an autoloading problem. If you modify some autoloading stuff you can always try a `composer install` which should regenerate the autoload files – mickadoo Nov 26 '18 at 16:18
  • @mickadoo could you give me a sample code? I am not very experienced with Laravel. The mail problem is that one of classes declared in `app/Vendors/MyBusiness.php` has to be used in `vendor/google/apiclient/src/Google/Http/REST.php` (see https://github.com/googleapis/google-api-php-client/blob/master/src/Google/Http/REST.php line 128) and is not visible. – pelcomppl Nov 27 '18 at 13:15
  • 1
    I'm not sure what the issue is, but it could be autoloading. If you have the `Google_Service_MyBusiness_Account` class inside `app/Vendors/MyBusiness.php` you could just make sure that things work first by manually adding this line to your index.php, or anywhere before the problem code is called: `require_once __DIR__ . '/path/to/MyBusiness.php';` (replacing the /path/to with the real path. If that works you could then move onto debugging why autoloading isn't working – mickadoo Nov 27 '18 at 15:45

2 Answers2

1

changed at function listAccounts(), line 1262

from:

return $this->call('list', array($params), "Google_Service_MyBusiness_ListAccountsResponse");

to:

return $this->call('list', array($params), "App\Services\Integrations\GoogleApi\Google_Service_MyBusiness_ListAccountsResponse");
0

The vendor directory is not within App/Vendor.

Try like this

$service = new \Google_Service_MyBusiness($client);
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45