2017-01-18 45 views
0

是否有可能使用服務帳戶和谷歌php api刪除用戶的別名?使用谷歌php api刪除別名

我嘗試了好幾種方法,包括:

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

$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); 

我得到任何錯誤401,需要登錄。或者錯誤,身份不明的'別名'

回答

1

它看起來像調用users-> delete vs users_aliases-> delete可能會導致問題。也許這樣的事情會起作用?

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); 

另請參見: https://developers.google.com/admin-sdk/directory/v1/reference/users/aliases/delete