From af1275d1c3dea166becd98da77777cb20b2d3df4 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Thu, 4 Aug 2016 13:54:07 +0200 Subject: [PATCH] Added feature to load credentials from file and updated README --- DependencyInjection/Configuration.php | 6 +++++ README.md | 38 +++++++++++++++++++++++++++ Services/GoogleClient.php | 8 +++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c3b0b76..5e188fa 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -180,6 +180,10 @@ function ($v) { break; case 'service': + if ((isset($v['getenv']) && true === $v['getenv']) || $v['json_file']) { + return $v; + } + $required = array('oauth2_client_email', 'oauth2_private_key', 'oauth2_scopes'); break; } @@ -205,6 +209,8 @@ function ($v) { ->variableNode('oauth2_scopes')->end() ->scalarNode('developer_key')->end() ->scalarNode('site_name')->end() + ->scalarNode('getenv')->end() + ->scalarNode('json_file')->end() ->scalarNode('authClass')->end() ->scalarNode('ioClass')->end() diff --git a/README.md b/README.md index f46826f..2c4bc6a 100644 --- a/README.md +++ b/README.md @@ -51,15 +51,53 @@ $bundles = array( ``` yaml # app/config/config.yml # you will get these parameters form https://code.google.com/apis/console/" +happy_r_google_api: + application_name: MySite + oauth2_client_id: + oauth2_client_secret: + oauth2_redirect_uri: + developer_key: + site_name: mysite.com +``` + +#### Advanced configuration + +You can set up multiple accounts, including service accounts. + +``` yaml +# app/config/config.yml happy_r_google_api: accounts: + + # regular web authentication default: + type: web application_name: MySite oauth2_client_id: oauth2_client_secret: oauth2_redirect_uri: developer_key: site_name: mysite.com + + # Credentials from GOOGLE_APPLICATION_CREDENTIALS environment variable (recommended) + service_env: + type: service + getenv: true + + # Credentials from service-account.json + service_file: + type: service + json_file: /path/to/your/service-account.json + + # with service credentials, example to access Google Analytics + service_account: + type: service + application_name: MySite + oauth2_client_id: + oauth2_client_email: + oauth2_private_key: + oauth2_scopes: + - https://www.googleapis.com/auth/analytics.readonly ``` diff --git a/Services/GoogleClient.php b/Services/GoogleClient.php index 48ca1e6..3e592a6 100644 --- a/Services/GoogleClient.php +++ b/Services/GoogleClient.php @@ -49,7 +49,13 @@ public function __construct(array $config, LoggerInterface $symfonyLogger = null case 'service': $client->setAccessType('offline'); - if (class_exists('\Google_Auth_AssertionCredentials')) { + if (isset($config['getenv']) && true === $config['getenv']) { + $client->useApplicationDefaultCredentials(); + + } else if (isset($config['json_file'])) { + $client->setAuthConfigFile($config['json_file']); + + } else if (class_exists('\Google_Auth_AssertionCredentials')) { //BC for Google API 1.0 $client->setAssertionCredentials( new \Google_Auth_AssertionCredentials(