使用google php api删除别名

时间:2017-01-18 21:33:27

标签: alias google-api-php-client

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

1 个答案:

答案 0 :(得分:1)

看起来呼叫用户 - > 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    = '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