Google-api-php-client: Loading credentials via environmental variables

Created on 14 Mar 2016  路  2Comments  路  Source: googleapis/google-api-php-client

Hi,

Thanks for the php library, it's been of great help.

I'm having an issue trying to use this library on a hosting service that doesn't support persistent disk storage and deploys via git push (like Heroku). This means I can't upload the credentials JSON file unless I put it in my .git repo, which I'd like to avoid for security reasons.

I see there is support for using environmental variables for Google Cloud platform. But I can't see any support for using environmental variables to store the actual credential values. I'd like to be able to set environmental variables like:

GOOGLE_API_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\n........
[email protected]

Would it be possible to adjust the setAuthConfig to read from environmental variables? I think it would be great and allow support on cloud hosting platforms.

Most helpful comment

@k0nG you don't need persistent disk - setAuthConfig supports an array argument. This allows flexibility with environment variables, or wherever you would like to store your credentials:

$auth = [
  "type" => "service_account",
  "project_id" => getenv('GOOGLE_PROJECT_ID'),
  "private_key" => getenv('GOOGLE_API_PRIVATE_KEY'),
  "client_email" => getenv('GOOGLE_API_CLIENT_EMAIL'),
  "client_id" => getenv('GOOGLE_API_CLIENT_ID'),
  "auth_uri" => "https://accounts.google.com/o/oauth2/auth",
  "token_uri" => "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url" => "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url" => getenv('GOOGLE_API_CLIENT_CERT_URL')
];

$client->setAuthConfig($auth);

All 2 comments

@k0nG you don't need persistent disk - setAuthConfig supports an array argument. This allows flexibility with environment variables, or wherever you would like to store your credentials:

$auth = [
  "type" => "service_account",
  "project_id" => getenv('GOOGLE_PROJECT_ID'),
  "private_key" => getenv('GOOGLE_API_PRIVATE_KEY'),
  "client_email" => getenv('GOOGLE_API_CLIENT_EMAIL'),
  "client_id" => getenv('GOOGLE_API_CLIENT_ID'),
  "auth_uri" => "https://accounts.google.com/o/oauth2/auth",
  "token_uri" => "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url" => "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url" => getenv('GOOGLE_API_CLIENT_CERT_URL')
];

$client->setAuthConfig($auth);

oh super. Sorry I had a look in the code and I couldn't see that.

Thanks for quick reply.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

camohub picture camohub  路  3Comments

usamamashkoor picture usamamashkoor  路  5Comments

bencromwell picture bencromwell  路  3Comments

unixkapl picture unixkapl  路  3Comments

whatido1 picture whatido1  路  3Comments