0

我正在查看 [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();

但后来一切都变得一团糟: 在此处输入图像描述

4

1 回答 1

0

这与 Google API 无关,而与 Java 类结构无关。考虑到您是 Java 初学者,您应该从更简单的练习开始。

Oracle 和 Internet 提供了大量关于 Java 的文档。您可以开始查看这些链接,例如: https ://docs.oracle.com/javase/tutorial/getStarted/application/ https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

于 2014-11-27T14:56:32.007 回答