9

Sto creando un'applicazione che consente all'amministratore di autenticare l'accesso al proprio account di analisi per l'utilizzo offline e di memorizzare il token di aggiornamento nel database.Come ottenere un token di accesso aggiornato utilizzando il token di aggiornamento memorizzato

Ora quando cerco di utilizzare l'API sul frontend, si restituisce il seguente errore:

"Access Token Expired. There wan a general error : The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved." 

Ecco il mio codice che genera questo errore finora:

require_once "lib/google/Google_Client.php"; 
require_once "lib/google/contrib/Google_AnalyticsService.php"; 

$_analytics = new analytics(); 
$_googleClient = new Google_Client(); 
$_googleClient->setClientId($_analytics->gaClientId); 
$_googleClient->setClientSecret($_analytics->gaClientSecret); 
$_googleClient->setRedirectUri($_analytics->gaRedirectUri); 
$_googleClient->setScopes($_analytics->gaScope); 
$_googleClient->setAccessType($_analytics->gaAccessType); 

// Returns last access token from the database (this works) 
$_tokenArray['access_token'] = $_analytics->dbAccessToken($_agencyId); 
$_googleClient->setAccessToken(json_encode($_tokenArray)); 

if($_googleClient->isAccessTokenExpired()) { 
    // Don't think this is required for Analytics API V3 
    //$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId)); 
    echo 'Access Token Expired'; // Debug 
} 

if (!$_googleClient->getAccessToken()) { 
    echo '<h2>Error - Admin has not setup analytics correct yet</h2>'; 
} 

che sto cercando una funzione per eseguire qualcosa come setRefreshToken - inserendo il valore dal database, dall'amministratore che lo ha autenticato in precedenza online.

risposta

7

È possibile provare quanto segue, è necessario aggiungere il codice per memorizzare il nuovo token nel database.

if($_googleClient->isAccessTokenExpired()) { 
    // Don't think this is required for Analytics API V3 
    //$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId)); 
    echo 'Access Token Expired'; // Debug 

    $_googleClient->authenticate(); 
    $NewAccessToken = json_decode($_googleClient->getAccessToken()); 
    $_googleClient->refreshToken($NewAccessToken->refresh_token); 
} 
+1

quello fissato esso, più stavo cercando di aggiornare il token utilizzando l'accesso t oken, invece di usare il token di aggiornamento come se fosse param. #fail - grazie! – mattpark22

+1

Nessuno di voi sa come posso forzare un token di accesso a scadere per testare utilizzando la funzione di aggiornamento dei token per ottenere un nuovo token di accesso? @mpark – Anagio

+1

@Anagio dovresti essere in grado di eseguire '$ _googleClient-> refreshToken ($ NewAccessToken-> refresh_token);' per forzare un nuovo token. Quindi confronta il vecchio token con quello nuovo per vedere se è cambiato. –

0

Utilizzare un try/catch e utilizzare il fermo per il reindirizzamento/aggiornamento del token di accesso. Sotto è la soluzione che ho usato per problema simile:

$plus = new Google_Service_Plus($client); 
try { 
$me = $plus->people->get('me'); 
} catch(Exception $e){ 
     if(!(strpos($_SERVER["REQUEST_URI"],'logout'))){ 
     if (isset($authUrl)){ 
       $redirect = $authUrl; 
     } 
     else{ 
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] .'?logout'; 
     } 
     header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); 
     exit; 
} 

È possibile sostituire l'istruzione sotto try con la linea di lanciare l'eccezione, credo che sarebbe

$_googleClient->setClientId($_analytics->gaClientId); 

o si può anche provare rinfrescante il token come da soluzione dato qui:

https://stackoverflow.com/a/22096740/1675384

Problemi correlati