6

Sto eseguendo il seguente codice PHP, utilizzando le librerie client trovate qui: https://code.google.com/p/google-api-php-client/. Non ricevo errori per nessuno di questo codice, ma quando chiamo getAccessToken(), restituisce null.Utilizzo di un account di servizio, getAccessToken() restituisce null

Ho autorizzato l'accesso a questo account di servizio sul mio calendario personale e ho concesso l'accesso completo al progetto tramite la Console API.

Qualche idea?

require_once 'google-api-php-client/src/Google_Client.php'; 

const CLIENT_ID = 'blahblahblah'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = 'path/to/privatekey.p12'; 

$google_client = new Google_Client(); // created only to initialized static dependencies 
$client = new Google_OAuth2(); // you really just need Google_OAuth2 

$key = file_get_contents(KEY_FILE); 

$client->setAssertionCredentials(
    new Google_AssertionCredentials(
     SERVICE_ACCOUNT_NAME, 
     array('https://www.googleapis.com/auth/calendar'), 
     $key 
    ) 
); 

var_dump($client->getAccessToken()); 

risposta

3

Per qualche ragione, questo sembrava funzionare:

require_once 'google-api-php-client/src/Google_Client.php'; 

const CLIENT_ID = 'blahblahblah'; 
const SERVICE_ACCOUNT_NAME = '[email protected]'; 
const KEY_FILE = 'path/to/privatekey.p12'; 
const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar"; 

$key = file_get_contents(KEY_FILE); 
$auth = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array(CALENDAR_SCOPE), 
    $key 
); 

$client = new Google_Client(); 
$client->setScopes(array(CALENDAR_SCOPE)); 
$client->setAssertionCredentials($auth); 
$client->getAuth()->refreshTokenWithAssertion(); 
$accessToken = $client->getAccessToken(); 

$client->setClientId(CLIENT_ID); 

Se qualcuno può spiegare il motivo per cui questo ha funzionato, si prega di modificare questa risposta o un commento!

+0

Non sicuro. Sto usando il tuo codice con credito su https://gist.github.com/fulldecent/6728257 e FYI la riga setScopes non è necessaria –

+1

'' getAccessToken() '' restituisce il token di accesso corrente, che non esiste finché non chiami '' refreshTokenWithAssertion() '' (per inizializzarlo) o '' setAccessToken() '' (se si è recuperato un token di accesso precedente che si desidera utilizzare). – tiho

Problemi correlati