如何创建具有多个别名的帐户?

How to create account with multiple aliases?

我正在尝试向我们的组织中插入一个新用户,而该用户有多个别名。 Google_Service_Directory_User 有能力 "setAliases"。在插入时,帐户已正确配置所有属性设置正确,减去别名 - 这些别名似乎被忽略了。

$dirObj = new Google_Service_Directory($client);

$primaryEmail  = 'joeschmo99@my.test.domain.com';
$alias1        = 'joetest1@my.test.domain.com';
$alias2        = 'joetest2@my.test.domain.com';

$firstName     = 'Joe';
$lastName      = 'Schmo99';
$shaPass       = sha1($someRandomPass);

$nameObject = new Google_Service_Directory_UserName();
$nameObject->setGivenName($firstName);
$nameObject->setFamilyName($lastName);
$nameObject->setFullName("$firstName $lastName");

$userObject = new Google_Service_Directory_User();
$userObject->setName($nameObject);
$userObject->setPassword($shaPass);
$userObject->setHashFunction('SHA-1');
$userObject->setPrimaryEmail($primaryEmail);
$userObject->setAliases( array( $alias1, $alias2 ));

$results = $dirObj->users->insert($userObject);

print_r($results);

对于插入带有别名的新用户有什么建议吗?

除了别名插入之外,您几乎做对了所有事情。让我们一步步过一遍。

  1. 首先创建目录对象,然后创建用户对象:

    $service = new Google_Service_Directory($client);
    
    $userObj = new Google_Service_Directory_User(
      array(
        "name" => array("givenName" => "Joe", "familyName" => "Schmo99"),
        "primaryEmail" => "joeschmo99@my.test.domain.com",
        "password" => $shaPass
      )
    );
    
  2. 接下来,您在帐户中插入用户对象并将返回的主电子邮件保存到变量中:

    $newUser = $service->users->insert($userObj);
    $createdUser = $newUser->primaryEmail;
    
  3. 创建一个要分配的用户别名数组:

    $userAliases = array("joetest1@my.test.domain.com", "joetest2@my.test.domain.com");
    
  4. 遍历数组,为每个别名电子邮件创建一个用户别名对象并将它们插入到用户对象中:

    foreach ($userAliases as $userAlias) {
    
        $newAlias = new Google_Service_Directory_Alias(
          array(
            "alias" => $userAlias,
          )
        );
    
        $service->users_aliases->insert($createdUser, $newAlias);
    
    }
    

请不要忘记查看有关如何插入别名的 official documentation 以获得更好、更详细的解释。我希望这有帮助! :)