Skip to content

Commit 3fbd152

Browse files
author
Maru Ahues Bouza
committed
Adding sample code to refresh token
1 parent 57dcc1c commit 3fbd152

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

examples/service-account.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,18 @@
6363
************************************************/
6464
if (isset($_SESSION['service_token'])) {
6565
$client->setAccessToken($_SESSION['service_token']);
66-
} else {
67-
$key = file_get_contents($key_file_location);
68-
$cred = new Google_Auth_AssertionCredentials(
69-
$service_account_name,
70-
array('https://www.googleapis.com/auth/books'),
71-
$key
72-
);
73-
// If we were retrieving on behalf of a specific delegated
74-
// user we should set the subject here.
75-
// $cred->sub = "emailaddress@yourdomain.com";
76-
$client->setAssertionCredentials($cred);
77-
$_SESSION['service_token'] = $client->getAccessToken();
7866
}
67+
$key = file_get_contents($key_file_location);
68+
$cred = new Google_Auth_AssertionCredentials(
69+
$service_account_name,
70+
array('https://www.googleapis.com/auth/books'),
71+
$key
72+
);
73+
$client->setAssertionCredentials($cred);
74+
if($client->getAuth()->isAccessTokenExpired()) {
75+
$client->getAuth()->refreshTokenWithAssertion($cred);
76+
}
77+
$_SESSION['service_token'] = $client->getAccessToken();
7978

8079
/************************************************
8180
We're just going to make the same call as in the

0 commit comments

Comments
 (0)