我正在查看 [this example] https://developers.google.com/admin-sdk/directory/v1/quickstart/quickstart-java )。
但我不想使用该 url 复制/粘贴版本进行身份验证,所以我读过我应该使用 serviceaccount。
我下载了 p12 密钥并将其放在我的源文件夹中。我正在使用在stackoverflow上找到的一些代码重建 Google 示例
所以这是我能走多远:
public class DirectoryCommandLine {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// Build service account credential.
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("......82p@developer.gserviceaccount.com")
.setServiceAccountScopes(DirectoryScopes.all())
.setServiceAccountPrivateKeyFromP12File(new java.io.File("eclipsejava-d6675et3ab71.p12"))
.build();
// Create a new authorized API client
Directory service = new Directory.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("DirectoryCommandLine")
.build();
List<User> allUsers = new ArrayList<User>();
Directory.Users.List request = service.users().list().setCustomer("my_customer");
// Get all users
do {
try {
Users currentPage = request.execute();
allUsers.addAll(currentPage.getUsers());
request.setPageToken(currentPage.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0 );
// Print all users
for (User currentUser : allUsers) {
System.out.println(currentUser.getPrimaryEmail());
}
}
}
如果我运行它,我会收到此错误:
Error: Main method not found in class DirectoryCommandLine, please define the main method as:
public static void main(String[] args)
我想我需要添加
public static void main(String[] args) throws IOException {
前/前:
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
但后来一切都变得一团糟: