Skip to content

Commit c95a3e9

Browse files
committed
Add service account support
1 parent abaa343 commit c95a3e9

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

src/Component.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,30 @@ private function getExtractor(): Extractor
298298

299299
private function getGoogleRestApi(): RestApi
300300
{
301-
$tokenData = json_decode($this->getConfig()->getOAuthApiData(), true);
302-
if (!isset($tokenData['access_token'], $tokenData['refresh_token'])) {
303-
throw new UserException('The token data are broken. Please try to reauthorize.');
304-
}
301+
$serviceAccount = $this->getConfig()->getServiceAccount();
302+
if ($serviceAccount) {
303+
$client = RestApi::createWithServiceAccount(
304+
$serviceAccount,
305+
[
306+
'https://www.googleapis.com/auth/analytics.readonly',
307+
],
308+
$this->getLogger(),
309+
);
310+
} else {
311+
/** @var array{access_token?: string, refresh_token?: string}|null $tokenData */
312+
$tokenData = json_decode($this->getConfig()->getOAuthApiData(), true);
313+
if (!isset($tokenData['access_token'], $tokenData['refresh_token'])) {
314+
throw new UserException('The token data are broken. Please try to reauthorize.');
315+
}
305316

306-
$client = new RestApi(
307-
$this->getConfig()->getOAuthApiAppKey(),
308-
$this->getConfig()->getOAuthApiAppSecret(),
309-
$tokenData['access_token'],
310-
$tokenData['refresh_token'],
311-
$this->getLogger(),
312-
);
317+
$client = RestApi::createWithOAuth(
318+
$this->getConfig()->getOAuthApiAppKey(),
319+
$this->getConfig()->getOAuthApiAppSecret(),
320+
$tokenData['access_token'],
321+
$tokenData['refresh_token'],
322+
$this->getLogger(),
323+
);
324+
}
313325

314326
$client->setBackoffsCount($this->getConfig()->getRetries());
315327
return $client;

0 commit comments

Comments
 (0)