Access token expires in a short time, apparently related to access security. If it is compromised, the access expires in a short time and with it the threat.
But there is the upgrade token , which can be used to update the session and thus generate another access token, contributing to a larger session.
Concept
// fazer operações de login...
// recolher o token de acesso
$_SESSION['token'] = $client->getAccessToken();
// se temos o token de acesso
if (isset($_SESSION['token']) && $_SESSION['token']!='') {
// definir um novo token de acesso
$client->setAccessToken($_SESSION['token']);
/* Descodificar o JSON que guardamos na variável de sessão
* e passar o mesmo para uma variável na forma de um objecto
*/
$sessionToken = json_decode($_SESSION['token']);
/* guardar o token de actualização num cookie com o nome "token",
* dando-lhe um tempo de vida maior
*/
setcookie("token", $sessionToken->refresh_token, time()+60*60*24*30); /* 1 mês de vida */
}
Refresh the session if you have to resort to a new login
When the access token is required, we can check the cookie:
-
If empty , we should request a new access token and a new update token through authentication;
-
If it is not empty , let's tell the client to update the token:
if isset($_COOKIE['token'] && $_COOKIE['token']!='') {
$client->refreshToken($_COOKIE['token']);
}
We're basically updating the access token with the help of the update token again.
Notes:
This response is an adaptation to an anonymous format of the response given by @hope_industries in SOEN.
Consult the same for the practical case that is treated there.