我有一个与 Google Apps for Work 域集成的应用程序,需要从 oauth 1 迁移到 oauth 2。
这是一个服务器应用程序,只需要:
- 列出域中的所有组。
- 列出指定组中的用户。
- 将成员添加到指定组。
- 从指定组中删除成员。
鉴于上述情况,我认为这应该使用服务帐户来完成。我创建了这个,下载了 P12 令牌(P12 和 JSON 令牌有什么区别?)并通过开发者控制台启用了 Admin SDK API。在域的控制面板中启用了 API 访问,并且我为与服务帐户关联的客户端 ID 启用了范围https://www.googleapis.com/auth/admin.directory.group.member。
我尝试了一些围绕组的随机操作,但得到“权限不足”的响应。
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
无论如何,首先,我正在寻找正确实现上述操作所需代码的一些帮助,然后看看是否仍然存在权限问题:
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;
import org.apache.commons.httpclient.HttpException;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.model.Group;
import com.google.api.services.admin.directory.model.Groups;
import com.google.api.services.admin.directory.model.Users;
public class GoogleAppsService {
HttpTransport httpTransport;
JsonFactory jsonFactory;
public GoogleAppsService() throws GeneralSecurityException, IOException {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
jsonFactory = JacksonFactory.getDefaultInstance();
}
public GoogleCredential getCredentials() throws HttpException, IOException, GeneralSecurityException {
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("179997031769-pf4t5hifo7dmtbqul1dbl9rulneijl7o@developer.gserviceaccount.com")
.setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/admin.directory.group.member"))
.setServiceAccountPrivateKeyFromP12File(
new File(this.getClass().getResource("/google_apps/google-apps-key.p12").getPath())).build();
return credential;
}
public void listGroups() throws Exception{
GoogleCredential credentials = getCredentials();
Directory directory = new Directory.Builder(
httpTransport, jsonFactory, credentials)
.setApplicationName("xyz")
.build();
//403 insufficient permissions thrown below is the above correct??
Groups result = directory.groups().list().execute();
System.out.println(result);
//iterate and print id/alias of each group
}
public void listUsers(String groupName) throws Exception {
GoogleCredential credentials = getCredentials();
//iterate and print email of each member for specified group
}
public void addUser(String groupname, String emailAddress)throws Exception {
GoogleCredential credentials = getCredentials();
}
public void removeUser(String groupName, String emailAddress)throws Exception {
GoogleCredential credentials = getCredentials();
}
public static void main(String[] args) throws Exception {
try {
GoogleAppsService service = new GoogleAppsService();
service.listGroups();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}