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();
}
}
}