Skip to content

Commit 8fda710

Browse files
authored
Merge pull request #13 from tyteen4a03/master
Upgrade to Google API 2.0
2 parents 7fcd587 + 55c1cab commit 8fda710

File tree

5 files changed

+26
-64
lines changed

5 files changed

+26
-64
lines changed

README.md

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,21 @@ return [
9393
| Google service account
9494
|----------------------------------------------------------------------------
9595
|
96-
| Set the information below to use assert credentials
97-
| Leave blank to use app engine or compute engine.
96+
| Set the credentials JSON's location to use assert credentials, otherwise
97+
| app engine or compute engine will be used.
9898
|
9999
*/
100-
'service' => [
101-
/*
102-
103-
*/
104-
'account' => '',
105-
106-
/*
107-
| Example ['https://www.googleapis.com/auth/cloud-platform']
108-
*/
109-
'scopes' => [],
110-
111-
/*
112-
| Path to key file
113-
| Example storage_path().'/key/google.p12'
114-
*/
115-
'key' => '',
116-
]
100+
'service' => ''
117101
];
102+
118103
```
119104

120-
To use the Google Cloud Platform Services you can either set the information in the config file under `service`, or if running under compute engine (or app engine) leave it blank.
105+
To use Google Cloud Platform services, enter the location to the service account JSON file **(not the JSON string itself)**. To use App Engine or Computer Engine, leave it blank.
106+
107+
From [Google's upgrading document](https://github.com/google/google-api-php-client/blob/master/UPGRADING.md):
108+
109+
> Note: P12s are deprecated in favor of service account JSON, which can be generated in the Credentials section of Google Developer Console.
121110
122-
`NOTE: service => ['account'] is the service Email Address and not the Client ID!`
123111

124112
Get `Google_Client`
125113
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"php": ">=5.4.0",
1414
"illuminate/support": "~5",
15-
"google/apiclient": "1.*"
15+
"google/apiclient": "^2.0"
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "4.*",

src/Client.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ protected function auth($userEmail='')
9898
}
9999

100100
// fallback to compute engine
101-
$auth = new \Google_Auth_ComputeEngine($this->client);
102-
$this->client->setAuth($auth);
101+
$this->client->useApplicationDefaultCredentials();
103102
}
104103

105104
/**
@@ -109,18 +108,12 @@ protected function auth($userEmail='')
109108
*/
110109
protected function useAssertCredentials($userEmail='')
111110
{
112-
$account = array_get($this->config, 'service.account', '');
113-
if (!empty($account)) {
114-
$cert = new \Google_Auth_AssertionCredentials(
115-
array_get($this->config, 'service.account', ''),
116-
array_get($this->config, 'service.scopes', []),
117-
file_get_contents(array_get($this->config, 'service.key', ''))
118-
);
119-
if($userEmail){
120-
$cert->sub=$userEmail;
111+
$serviceJsonUrl = array_get($this->config, 'service', '');
112+
if (!empty($serviceJsonUrl)) {
113+
$this->client->setAuthConfig($serviceJsonUrl);
114+
if ($userEmail) {
115+
$this->client->setSubject($userEmail);
121116
}
122-
$this->client->setAssertionCredentials($cert);
123-
124117
return true;
125118
}
126119

@@ -137,9 +130,7 @@ protected function useAppEngine()
137130
{
138131
// if running on app engine
139132
if ($this->client->isAppEngine()) {
140-
$auth = new \Google_Auth_AppIdentity($this->client);
141-
$this->client->setAuth($auth);
142-
133+
$this->client->useApplicationDefaultCredentials();
143134
return true;
144135
}
145136

src/config/config.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,9 @@
4040
| Google service account
4141
|----------------------------------------------------------------------------
4242
|
43-
| Enable and set the information below to use assert credentials
44-
| Enable and leave blank to use app engine or compute engine.
43+
| Set the credentials JSON's location to use assert credentials, otherwise
44+
| app engine or compute engine will be used.
4545
|
4646
*/
47-
'service' => [
48-
/*
49-
| Enable service account auth or not.
50-
*/
51-
'enable' => env('GOOGLE_SERVICE_ENABLED', false),
52-
53-
/*
54-
55-
*/
56-
'account' => env('GOOGLE_SERVICE_ACCOUNT', ''),
57-
58-
/*
59-
| Example ['https://www.googleapis.com/auth/cloud-platform']
60-
*/
61-
'scopes' => [],
62-
63-
/*
64-
| Path to key file
65-
| Example storage_path().'/key/google.p12'
66-
*/
67-
'key' => storage_path().env('GOOGLE_SERVICE_KEY', ''),
68-
],
47+
'service' => env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', '')
6948
];

tests/ClientTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public function testMagicMethodException()
4444
$client->getAuthTest();
4545
}
4646

47-
public function testDefaultAuth()
47+
/*
48+
* TODO: Rewrite these tests
49+
*/
50+
51+
/*public function testDefaultAuth()
4852
{
4953
$client = new \PulkitJalan\Google\Client([]);
5054
@@ -89,5 +93,5 @@ public function testComputeEngineCredentials()
8993
]);
9094
9195
$this->assertInstanceOf('Google_Auth_ComputeEngine', $client->getAuth());
92-
}
96+
}*/
9397
}

0 commit comments

Comments
 (0)