2017-01-18 4 views
0

Est-il possible de supprimer l'alias d'un utilisateur à l'aide d'un compte de service et de l'API google php?Supprimer un alias à l'aide de google php api

J'ai essayé plusieurs méthodes, dont:

$optParams = array('alias'=>$userMail); 
    $remove_alias = $directory_service->users->delete($user_info['primaryEmail'],$optParams); 
    $remove1 = $directory_service->getAuth()->authenticatedRequest($remove_alias); 

ET

$url = "https://www.googleapis.com/admin/directory/v1/users/" . $user_info['primaryEmail'] . "/aliases/" . $userMail; 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $request_headers = array(); 
    $request_headers['Content-type'] = 'application/json'; 

    curl_setopt($ch, CURLINFO_HEADER_OUT, 1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 


    $output = curl_exec($ch); 

    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 
    $header = substr($output, 0, $header_size); 
    $body = substr($output, $header_size); 

    curl_close($ch); 

je reçois soit erreur 401, connexion requise. Ou erreur, 'alias' non identifié

Répondre

1

Il semble que l'appel des utilisateurs-> delete vs users_aliases-> delete soit à l'origine du problème. Peut-être que quelque chose comme ça fonctionnerait?

require_once $CLIENT_API_PATH . '/vendor/autoload.php'; 
date_default_timezone_set('America/Los_Angeles'); 

// this is the service account json file used to make api calls within the domain 
$serviceAccount = $JSON_PATH . '/service-account-creds.json'; 

// delegate the work to an account with ability to make changes 
$impersonateUser = '[email protected]'; 

// these are the scope(s) used. 
define('SCOPES', implode(' ', array(Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS))); 

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $serviceAccount); 

$client = new Google_Client(); 

// loads the json service account file. 
$client->useApplicationDefaultCredentials(); 
$client->setScopes(SCOPES); 
$client->setSubject($impersonateUser); 

$dirObj = new Google_Service_Directory($client); 

// This is the user account to delete the alias from 
$userKey = '[email protected]'; 
$alias = '[email protected]'; 
$aliasObj = new Google_Service_Directory_Alias(array('alias' => $alias)); 

// delete the alias from the account. 
$results = $dirObj->users_aliases->delete($userKey, $aliasObj); 

// If successful, this method returns an empty response body. 
print_r($results); 

Voir aussi: https://developers.google.com/admin-sdk/directory/v1/reference/users/aliases/delete