如何创建包含多个别名的帐户?

时间:2017-01-18 01:54:22

标签: php google-api google-api-php-client google-directory-api

我正在尝试将新用户插入我们的组织,此用户有多个别名。 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 个答案:

答案 0 :(得分:1)

除了别名插入之外,您几乎正在做所有事情。让我们一步一步来看看。

  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);
    
    }
    
  5. 请不要忘记查看有关如何插入别名的official documentation以获得更好和更详细的说明。我希望这有帮助! :)