使用谷歌PHP API删除别名



是否可以使用服务帐户和Google 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,需要登录。或错误,无法识别的"别名"

看起来调用用户->删除与users_aliases->删除可能会导致此问题。 也许这样的事情会起作用?

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    = 'adminWithRolesToMakeChanges@your.domain.com';
// 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 = 'joeschmo@your.domain.com';
$alias   = 'alias2delete@your.domain.com';
$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

最新更新