0

I have been trying to use the google api client in cakekphp 3, but not successful on adding the service account json file to my project.

The below line is mentioned in:

https://github.com/googleapis/google-api-php-client

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');

But that doesn't work on cakephp 3. I know I can add it to the .env.default file, but it is not recommended for production.

How to use it then?

udarts
  • 745
  • 1
  • 8
  • 23
  • 1
    `putenv()` is a vanilla PHP function, there's no reason why it shouldn't work with CakePHP. – ndm Aug 25 '19 at 15:34
  • But where do you add it? bootstrap.php? Or app.php – udarts Aug 25 '19 at 16:00
  • Both would work... what the best place is would depend on whether the path differs per environment, and whether you might be setting the variable at server level in one of your environments. – ndm Aug 25 '19 at 16:09

1 Answers1

0

This function defines a default credentials setting path to be used, but note that you have not called it yet.

You must "tell" your application to use its default credentials settings.

// app.php
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
// In the file you want to use Google Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();

CakePHP also can read an specific file for env variables:

// config/.env
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
// Uncomment this part of your bootstrap.php
if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
     $dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
     $dotenv->parse()
        ->putenv()
        ->toEnv()
        ->toServer();
}

Keeping your setup on .env files make it easier maintaining all your settings up to date.