|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | // [START admin_sdk_directory_quickstart]
|
| 16 | + |
16 | 17 | import com.google.api.client.auth.oauth2.Credential;
|
17 | 18 | import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
|
18 | 19 | import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
|
|
27 | 28 | import com.google.api.services.admin.directory.DirectoryScopes;
|
28 | 29 | import com.google.api.services.admin.directory.model.User;
|
29 | 30 | import com.google.api.services.admin.directory.model.Users;
|
30 |
| - |
31 | 31 | import java.io.FileNotFoundException;
|
32 | 32 | import java.io.IOException;
|
33 | 33 | import java.io.InputStream;
|
|
37 | 37 | import java.util.List;
|
38 | 38 |
|
39 | 39 | public class AdminSDKDirectoryQuickstart {
|
40 |
| - /** Application name. */ |
41 |
| - private static final String APPLICATION_NAME = "Google Admin SDK Directory API Java Quickstart"; |
42 |
| - /** Global instance of the JSON factory. */ |
43 |
| - private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); |
44 |
| - /** Directory to store authorization tokens for this application. */ |
45 |
| - private static final String TOKENS_DIRECTORY_PATH = "tokens"; |
| 40 | + /** |
| 41 | + * Application name. |
| 42 | + */ |
| 43 | + private static final String APPLICATION_NAME = "Google Admin SDK Directory API Java Quickstart"; |
| 44 | + /** |
| 45 | + * Global instance of the JSON factory. |
| 46 | + */ |
| 47 | + private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); |
| 48 | + /** |
| 49 | + * Directory to store authorization tokens for this application. |
| 50 | + */ |
| 51 | + private static final String TOKENS_DIRECTORY_PATH = "tokens"; |
46 | 52 |
|
47 |
| - /** |
48 |
| - * Global instance of the scopes required by this quickstart. |
49 |
| - * If modifying these scopes, delete your previously saved tokens/ folder. |
50 |
| - */ |
51 |
| - private static final List<String> SCOPES = Collections.singletonList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY); |
52 |
| - private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; |
| 53 | + /** |
| 54 | + * Global instance of the scopes required by this quickstart. |
| 55 | + * If modifying these scopes, delete your previously saved tokens/ folder. |
| 56 | + */ |
| 57 | + private static final List<String> SCOPES = |
| 58 | + Collections.singletonList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY); |
| 59 | + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; |
53 | 60 |
|
54 |
| - /** |
55 |
| - * Creates an authorized Credential object. |
56 |
| - * @param HTTP_TRANSPORT The network HTTP Transport. |
57 |
| - * @return An authorized Credential object. |
58 |
| - * @throws IOException If the credentials.json file cannot be found. |
59 |
| - */ |
60 |
| - private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException { |
61 |
| - // Load client secrets. |
62 |
| - InputStream in = AdminSDKDirectoryQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); |
63 |
| - if (in == null) { |
64 |
| - throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); |
65 |
| - } |
66 |
| - GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
67 |
| - |
68 |
| - // Build flow and trigger user authorization request. |
69 |
| - GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( |
70 |
| - HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
71 |
| - .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) |
72 |
| - .setAccessType("offline") |
73 |
| - .build(); |
74 |
| - LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); |
75 |
| - return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); |
| 61 | + /** |
| 62 | + * Creates an authorized Credential object. |
| 63 | + * |
| 64 | + * @param HTTP_TRANSPORT The network HTTP Transport. |
| 65 | + * @return An authorized Credential object. |
| 66 | + * @throws IOException If the credentials.json file cannot be found. |
| 67 | + */ |
| 68 | + private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) |
| 69 | + throws IOException { |
| 70 | + // Load client secrets. |
| 71 | + InputStream in = AdminSDKDirectoryQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); |
| 72 | + if (in == null) { |
| 73 | + throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); |
76 | 74 | }
|
| 75 | + GoogleClientSecrets clientSecrets = |
| 76 | + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
| 77 | + |
| 78 | + // Build flow and trigger user authorization request. |
| 79 | + GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( |
| 80 | + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
| 81 | + .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) |
| 82 | + .setAccessType("offline") |
| 83 | + .build(); |
| 84 | + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); |
| 85 | + return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); |
| 86 | + } |
77 | 87 |
|
78 |
| - public static void main(String... args) throws IOException, GeneralSecurityException { |
79 |
| - // Build a new authorized API client service. |
80 |
| - final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
81 |
| - Directory service = new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) |
82 |
| - .setApplicationName(APPLICATION_NAME) |
83 |
| - .build(); |
| 88 | + public static void main(String... args) throws IOException, GeneralSecurityException { |
| 89 | + // Build a new authorized API client service. |
| 90 | + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
| 91 | + Directory service = |
| 92 | + new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) |
| 93 | + .setApplicationName(APPLICATION_NAME) |
| 94 | + .build(); |
84 | 95 |
|
85 |
| - // Print the first 10 users in the domain. |
86 |
| - Users result = service.users().list() |
87 |
| - .setCustomer("my_customer") |
88 |
| - .setMaxResults(10) |
89 |
| - .setOrderBy("email") |
90 |
| - .execute(); |
91 |
| - List<User> users = result.getUsers(); |
92 |
| - if (users == null || users.size() == 0) { |
93 |
| - System.out.println("No users found."); |
94 |
| - } else { |
95 |
| - System.out.println("Users:"); |
96 |
| - for (User user : users) { |
97 |
| - System.out.println(user.getName().getFullName()); |
98 |
| - } |
99 |
| - } |
| 96 | + // Print the first 10 users in the domain. |
| 97 | + Users result = service.users().list() |
| 98 | + .setCustomer("my_customer") |
| 99 | + .setMaxResults(10) |
| 100 | + .setOrderBy("email") |
| 101 | + .execute(); |
| 102 | + List<User> users = result.getUsers(); |
| 103 | + if (users == null || users.size() == 0) { |
| 104 | + System.out.println("No users found."); |
| 105 | + } else { |
| 106 | + System.out.println("Users:"); |
| 107 | + for (User user : users) { |
| 108 | + System.out.println(user.getName().getFullName()); |
| 109 | + } |
100 | 110 | }
|
| 111 | + } |
101 | 112 | }
|
102 | 113 | // [END admin_sdk_directory_quickstart]
|
0 commit comments