Google Directory API:可以创建用户,获取用户列表,但不能检索单个用户

时间:2018-06-25 18:38:36

标签: php google-directory-api

我有G Suite帐户,正在使用Super Admin用户对API进行身份验证。这是我的代码:

<StackPanel>
    <TextBlock TextWrapping="Wrap" Text="Here is some manual Text"/>
</StackPanel>

使用此身份验证功能,我可以成功地做到这一点:

   private static function authGoogle() {
    $base_path = base_path();
    $client = new Google_Client();
    $client->setScopes([
        Google_Service_Directory::ADMIN_DIRECTORY_USER,
        Google_Service_Directory::ADMIN_DIRECTORY_GROUP,
        Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER
    ]);
    try {
        $client->setAuthConfig($base_path.'/client_secret.json');
    }
    catch (\Google_Exception $ex) {
        return $ex->getMessage();
    }
    $client->setAccessType('offline');

    $credentialsPath = $base_path.'/credentials.json';
    if (file_exists($credentialsPath)) {
        $accessToken = json_decode(file_get_contents($credentialsPath), true);
    } else {
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n%s\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));
        $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
        if (!file_exists(dirname($credentialsPath))) {
            mkdir(dirname($credentialsPath), 0700, true);
        }
        file_put_contents($credentialsPath, json_encode($accessToken));
        printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client->setAccessToken($accessToken);
    if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
    }
    return $client;
}

这非常有效,可以添加用户,设置密码并在下次登录/首次登录时要求更改密码。同样,当我使用目录服务 $client = self::authGoogle(); $service = new Google_Service_Directory($client); $postBody = new Google_Service_Directory_User(); $postBody->setPrimaryEmail($username.'@mydomain.com'); $postBody->setPassword($password); $name = new Google_Service_Directory_UserName(); $name->setFamilyName($last_name); $name->setGivenName($first_name); $postBody->setChangePasswordAtNextLogin(true); $postBody->setName($name); $user = $service->users->insert($postBody); return json_encode($user); 时,也会收到用户列表的响应,但是当我尝试执行此操作时:

$service->users->listUsers($params)->getUsers();

我无法通过并得到以下答复:

    $client = self::authGoogle();
    $service = new Google_Service_Directory($client);
    return json_encode($service->users->get($email));

我想念什么? Google方面的API资源管理器说,拥有ADMIN_DIRECTORY_USER就足够了,我可以再次创建用户,可以检索用户列表,但不能以特权不足而无法获得单个用户。

0 个答案:

没有答案