token was not refreshing
This commit is contained in:
parent
75fbebbcb6
commit
b748665296
@ -984,38 +984,28 @@ class CMailFile
|
|||||||
$storage = new DoliStorage($db, $conf);
|
$storage = new DoliStorage($db, $conf);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$token_ok = true;
|
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
|
||||||
try {
|
|
||||||
$tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->errors[] = $e->getMessage();
|
|
||||||
$token_ok = false;
|
|
||||||
}
|
|
||||||
$expire = false;
|
$expire = false;
|
||||||
// Is token expired or will token expire in the next 30 seconds
|
// Is token expired or will token expire in the next 30 seconds
|
||||||
if (is_object($tokenobj)) {
|
if (is_object($tokenobj)) {
|
||||||
$expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
|
$expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
|
||||||
}
|
}
|
||||||
// Token expired so we refresh it
|
// Token expired so we refresh it
|
||||||
if ($token_ok && $expire) {
|
if (is_object($tokenobj) && $expire) {
|
||||||
try {
|
$credentials = new Credentials(
|
||||||
$credentials = new Credentials(
|
getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_ID'),
|
||||||
getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_ID'),
|
getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_SECRET'),
|
||||||
getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_SECRET'),
|
getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_URLAUTHORIZE')
|
||||||
getDolGlobalString('OAUTH_'.$conf->global->MAIN_MAIL_SMTPS_OAUTH_SERVICE.'_URLAUTHORIZE')
|
);
|
||||||
);
|
$serviceFactory = new \OAuth\ServiceFactory();
|
||||||
$serviceFactory = new \OAuth\ServiceFactory();
|
$oauthname = explode('-', $OAUTH_SERVICENAME);
|
||||||
$oauthname = explode('-', $OAUTH_SERVICENAME);
|
// ex service is Google-Emails we need only the first part Google
|
||||||
// ex service is Google-Emails we need only the first part Google
|
$apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
|
||||||
$apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
|
// il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
|
||||||
// il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
|
$refreshtoken = $tokenobj->getRefreshToken();
|
||||||
$refreshtoken = $tokenobj->getRefreshToken();
|
$tokenobj = $apiService->refreshAccessToken($tokenobj);
|
||||||
$tokenobj = $apiService->refreshAccessToken($tokenobj);
|
$tokenobj->setRefreshToken($refreshtoken);
|
||||||
$tokenobj->setRefreshToken($refreshtoken);
|
$storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
|
||||||
$storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->errors[] = $e->getMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (is_object($tokenobj)) {
|
if (is_object($tokenobj)) {
|
||||||
$this->transport->setAuthMode('XOAUTH2');
|
$this->transport->setAuthMode('XOAUTH2');
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user