Adding google api config file to environment

Hello,

I am trying to hook up google api access to my site. I have created a credentials file that I need to add to env.

I have added other config values in wordpress_sites.yml and vault just fine but this time it is a file with multiple values that needs to be added like this:

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

The file itself is something like this:

{
  "type": "service_account",
  "project_id": "xxx",
  "private_key_id": "xxx",
  "private_key": "-----BEGIN PRIVATE KEY-----xxx-----END PRIVATE KEY-----\n",
  "client_email": "xxx",
  "client_id": "xx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "xxx"
}

I want the file outside of the root and have no idea how to do it.

Anybody know?

Thanks,
Josh

You could store everything inside of the JSON file as separate values in env then make an endpoint that returns JSON using the values you need.

You could store everything inside of the JSON file as separate values in env then make an endpoint that returns JSON using the values you need.

Sorry to be kurt but: don’t do this haha.

  1. put the file somewhere. Maybe in the config directory.
  2. in your application.php define a config constant named “GOOGLE_APPLICATION_CREDENTIALS” and make the value as realpath('./service-account.json')
  3. Feel free to add conditional logic like “service-account-staging.json” and stuff
1 Like

Fair enough! How comes?

The api client expects a path to a file: https://github.com/googleapis/google-api-php-client/blob/master/README.md#authentication-with-service-accounts

You don’t want to care about what is in the JSON file, just feed it to the api client

1 Like

Thanks so much guys.

I did something similar to Austin’s approach and it seems to be working.

Files in config seem to be added to the site directory just fine and in my test php I have:

require_once __DIR__.'/../../../../vendor/autoload.php';
$client->setAuthConfig(__DIR__ . '/../../../../config/client_secrets.json');

Those lines seem to be working fine.
@austin - your method might be a bit cleaner but is there any possible issues with my approach?

Thank you,
Josh

Seems fine! I have the same thing in my codebase

This topic was automatically closed after 42 days. New replies are no longer allowed.