使用 Google 目录管理员 API 查找 UserKey 时出错

Error finding UserKey with Google Directory Admin API

我有一些代码可以检索、删除、更新和创建 google 用户。我可以使用以下方法毫无问题地创建用户:

public void createUser(String id, String firstName, String lastName, String email, String password){

    User user = new User();

    try{


        UserName name = new UserName();
        name.setFamilyName(lastName);
        name.setGivenName(firstName);
        user.setName(name);
        user.setPassword(password);
        user.setPrimaryEmail(email);
        user.setId(id);

        System.out.println(service.users().insert(user).execute());         
    }
    catch(Exception e){
        System.out.println("Error creating user: " + e);
    }

我还可以通过以下方式检索用户密钥:

System.out.println("UserKey: " + service.users().get(id).getUserKey());

但是当我尝试更新或删除具有该 ID 的用户时,出现以下错误:

 com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
  "code" : 404,
  "errors" : [ {
  "domain" : "global",
  "message" : "Resource Not Found: userKey",
  "reason" : "notFound"
} ],
"message" : "Resource Not Found: userKey"
}

我的删除方法如下:

public void deleteUser(String id){
    try{
        System.out.println(service.users().delete(id).execute());
    }
    catch(Exception e){
        System.out.println("Error deleting user: " + e);
    }
}

如果我将电子邮件地址 (id@domain.edu) 传递给删除或更新方法,它会找到该帐户并正常工作,但我需要能够仅传递我创建该帐户所用的 ID在插入方法中。有什么想法吗?

调用目录 API 时,您必须始终指定用户的完整电子邮件地址或用户的 Google 唯一 ID。仅提供用户名的本地部分是不够的,因为这不允许 Google 确定用户与哪个 Google Apps 域关联。