使用Google Admin SDK进行群组和成员管理

时间:2017-03-02 10:32:19

标签: java google-admin-sdk

使用Google API管理会员时遇到一些问题。 这段代码实际上运行正常:

    Collection<String> scopes = new LinkedList<String>();
    scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER_ALIAS);
    scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER);

    String adminMail = "admin.google@mydomain.com"; //This is a Super Admin User
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId(serviceMail).setServiceAccountScopes(scopes).setServiceAccountUser(adminMail).setServiceAccountPrivateKeyFromP12File(new File(privateKey)).build();

    Directory directory = new Directory.Builder(httpTransport, jsonFactory, credential).setApplicationName(applicationName).build();    
    //User Management   
    User myUser= directory.users().get("admin.google@mydomain.com").execute(); //IT WORKS !!

    //Group Management
    String userEmail = "myMail@mydomain.com";
    Member member = new Member();
    member.setEmail(userEmail);
    member.setRole("MEMBER");
    String groupID = "testGroup1@mydomain.com";
    directory.members().insert(groupID, member).execute(); //DOES NOT WORK

但是当我尝试管理一个或多个成员时,我获得了此权限错误:

  com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Insufficient Permission",
    "reason" : "insufficientPermissions"
  } ],
  "message" : "Insufficient Permission"
}

如果我尝试添加以下范围:

Collection<String> scopes = new LinkedList<String>();
scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER_ALIAS);
scopes.add(DirectoryScopes.ADMIN_DIRECTORY_USER);
scopes.add(DirectoryScopes.ADMIN_DIRECTORY_GROUP);
scopes.add(DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER);

我收到此错误:

java.lang.NullPointerException
 com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
 com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
 com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
 com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
 com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
 com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
 com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
 com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
 com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269)
 com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
 com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
 main.java.MioMain.main(MioMain.java:77)

用户搜索和群组成员插入不起作用。 你能帮帮我吗?

0 个答案:

没有答案