2013-03-06 29 views
5

Sto utilizzando google-api-php-client 0.6.1 e mi piacerebbe sapere c'è un modo per impersonare un utente concreto con un account di servizio? La mia applicazione ha bisogno di memorizzare alcuni file nella sua unità di google. Quindi, ho deciso di utilizzare l'account di servizio utente e la chiave .p12: l'autenticazione. Funziona alla grande, ma tutti i file vengono archiviati nell'account di servizio, quindi non posso gestirli. Vorrei che i documenti fossero archiviati in un determinato account (che stava usando per creare il progetto API e l'account di servizio stesso). Stavo cercando di utilizzare questo codice:Impersonare l'utente google con un account di servizio

$KEY_FILE = <p12 key file path>; 
$key = file_get_contents($KEY_FILE); 
$auth = new Google_AssertionCredentials(
     $SERVICE_ACCOUNT_NAME, 
     array('https://www.googleapis.com/auth/drive'), 
     $key); 
$auth->prn = '<[email protected]>'; 
$client = new Google_Client(); 
$client->setUseObjects(true); 
$client->setAssertionCredentials($auth); 
return new Google_DriveService($client); 

ma ho ottenuto "Errore rinfrescante il token OAuth2, un messaggio: '{ "errore": "ACCESS_DENIED"}'"

+0

Ho riscontrato lo stesso problema e sembra che dovrei apportare alcune modifiche alla stessa configurazione di google. Hai trovato la soluzione? Puoi indicarmi cosa dovrei cambiare? – Sergei

risposta

1

Non utente $ auth- > prn, usa $ auth-> sub. Questo funziona per me:

// Create a new google client. We need this for all API access. 
$client = new Google_Client(); 
$client->setApplicationName("Google Group Test"); 

$client_id = '...'; 
$service_account_name = '...'; 
$key_file_location = '...'; 

if (isset($_SESSION['service_token'])) { 
    $client->setAccessToken($_SESSION['service_token']); 
} 
$key = file_get_contents($key_file_location); 

// https://www.googleapis.com/auth/admin.directory.group, 
// https://www.googleapis.com/auth/admin.directory.group.readonly, 
// https://www.googleapis.com/auth/admin.directory.group.member, 
// https://www.googleapis.com/auth/admin.directory.group.member.readonly, 
// https://www.googleapis.com/auth/apps.groups.settings, 
// https://www.googleapis.com/auth/books 
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
     array(
      Google_Service_Groupssettings::APPS_GROUPS_SETTINGS, 
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP, 
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_READONLY, 

      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER, 
      Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER_READONLY, 

      Google_Service_Books::BOOKS, 
     ), 
     $key, 
     'notasecret' 
    ); 
// 
// Very important step: the service account must also declare the 
// identity (via email address) of a user with admin priviledges that 
// it would like to masquerade as. 
// 
// See: http://stackoverflow.com/questions/22772725/trouble-making-authenticated-calls-to-google-api-via-oauth 
// 
$cred->sub = '...'; 
$client->setAssertionCredentials($cred); 
if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 
$_SESSION['service_token'] = $client->getAccessToken(); 
Problemi correlati