Skip to content

Commit

Permalink
Added feature to load credentials from file and updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Aug 4, 2016
1 parent 0862e7e commit 82ee6ff
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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()
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
8 changes: 7 additions & 1 deletion Services/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 82ee6ff

Please sign in to comment.