Google Cloud Print

This commit is contained in:
frederic34
2015-11-05 18:04:00 +01:00
parent 1927f47eaa
commit 3ef6a92bb1
4 changed files with 77 additions and 25 deletions

View File

@@ -30,6 +30,8 @@ use OAuth\Common\Consumer\Credentials;
use OAuth\Common\Token\TokenInterface;
use OAuth\OAuth2\Service\Google;
$action = GETPOST('action', 'alpha');
/**
* Create a new instance of the URI class with the current URI, stripping the query string
*/
@@ -59,45 +61,73 @@ $apiService = $serviceFactory->createService('Google', $credentials, $storage, a
// access type needed for google refresh token
$apiService->setAccessType('offline');
//print '<pre>'.print_r($apiService,true).'</pre>';
//print 'Has access Token: '.($storage->hasAccessToken('Google')?'Yes':'No').'</ br>';
//print 'Has Author State: '.($storage->hasAuthorizationState('Google')?'Yes':'No').'</ br>';
//print 'Authorization State: '.$storage->retrieveAuthorizationState('Google').'</ br>';
//print '<td><pre>'.print_r($token,true).'</pre></td>';
if (! empty($_GET['code'])) {
if ($action == 'delete') {
// delete token
llxHeader('',$langs->trans("OAuthSetup"));
$linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans("OAuthSetup"),$linkback,'title_setup');
dol_fiche_head();
$storage->clearToken('Google');
dol_fiche_end();
} elseif (! empty($_GET['code'])) {
llxHeader('',$langs->trans("OAuthSetup"));
$linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans("OAuthSetup"),$linkback,'title_setup');
dol_fiche_head();
// retrieve the CSRF state parameter
$state = isset($_GET['state']) ? $_GET['state'] : null;
print '<table>';
// looking for a token already stored in db
//try {
// $token = $storage->retrieveAccessToken('Google');
// $old_token=1;
//} catch (Exception $e) {
// $old_token=0;
//}
//if ($old_token==1) {
// print '<tr><td>'.$langs->trans('OldTokenStored').'</td><td></td></tr>';
// print '<tr><td><pre>'.print_r($token,true).'</pre></td></tr>';
//}
//$refreshtoken = $token->getRefreshToken();
// This was a callback request from service, get the token
try {
$token = $storage->retrieveAccessToken('Google');
$apiService->requestAccessToken($_GET['code'], $state);
} catch (Exception $e) {
print $e->getMessage();
}
//print '<pre>'.print_r($token->getRefreshToken(),true).'</pre>';
//$refreshtoken = $token->getRefreshToken();
// This was a callback request from service, get the token
$apiService->requestAccessToken($_GET['code'], $state);
//print '<pre>'.print_r($apiService,true).'</pre>';
// retrieve new token in db
try {
$token = $storage->retrieveAccessToken('Google');
$new_token=1;
} catch (Exception $e) {
print $e->getMessage();
$new_token=0;
}
$newrefreshtoken = $token->getRefreshToken();
if (empty($newrefreshtoken) && ! empty($refreshtoken)) {
$token->setRefreshToken($refreshtoken);
$storage->storeAccessToken('Google', $token);
}
print '<td><pre>'.print_r($token,true).'</pre></td>';
if ($new_token==1) {
print '<tr><td>'.$langs->trans('NewTokenStored').'</td><td></td></tr>';
print '<tr><td><pre>'.print_r($token,true).'</pre></td></tr>';
}
//print '<td><pre>'.print_r($token,true).'</pre></td>';
//$apiService->refreshAccessToken($token);
//print '<pre>'.print_r($apiService,true).'</pre>';
//$token = $storage->retrieveAccessToken('Google');
//print '<td><pre>'.print_r($token,true).'</pre></td>';
print '<td><a href="https://security.google.com/settings/security/permissions" target="_blank">Applications associées à votre compte</a></td>';
print '</table>';
dol_fiche_end();
} else {
$url = $apiService->getAuthorizationUri();
// we go on google authorization page