|
18 | 18 | import java.util.stream.Collectors; |
19 | 19 |
|
20 | 20 | public class DriveActivityQuickstart { |
21 | | - /** Application name. */ |
22 | | - private static final String APPLICATION_NAME = "G Suite Activity API Java Quickstart"; |
23 | | - |
24 | | - /** Directory to store authorization tokens for this application. */ |
25 | | - private static final java.io.File DATA_STORE_DIR = new java.io.File("tokens"); |
26 | | - |
27 | | - /** Global instance of the {@link FileDataStoreFactory}. */ |
28 | | - private static FileDataStoreFactory DATA_STORE_FACTORY; |
29 | | - |
30 | | - /** Global instance of the JSON factory. */ |
31 | | - private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
32 | | - |
33 | | - /** Global instance of the HTTP transport. */ |
34 | | - private static HttpTransport HTTP_TRANSPORT; |
35 | | - |
36 | | - /** |
37 | | - * Global instance of the scopes required by this quickstart. |
38 | | - * |
39 | | - * <p>If modifying these scopes, delete your previously saved credentials at |
40 | | - * ~/.credentials/driveactivity-java-quickstart |
41 | | - */ |
42 | | - private static final List<String> SCOPES = |
43 | | - Arrays.asList(DriveActivityScopes.DRIVE_ACTIVITY_READONLY); |
44 | | - |
45 | | - static { |
46 | | - try { |
47 | | - HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
48 | | - DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); |
49 | | - } catch (Throwable t) { |
50 | | - t.printStackTrace(); |
51 | | - System.exit(1); |
| 21 | + /** Application name. */ |
| 22 | + private static final String APPLICATION_NAME = "Drive Activity API Java Quickstart"; |
| 23 | + |
| 24 | + /** Directory to store authorization tokens for this application. */ |
| 25 | + private static final java.io.File DATA_STORE_DIR = new java.io.File("tokens"); |
| 26 | + |
| 27 | + /** Global instance of the {@link FileDataStoreFactory}. */ |
| 28 | + private static FileDataStoreFactory DATA_STORE_FACTORY; |
| 29 | + |
| 30 | + /** Global instance of the JSON factory. */ |
| 31 | + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
| 32 | + |
| 33 | + /** Global instance of the HTTP transport. */ |
| 34 | + private static HttpTransport HTTP_TRANSPORT; |
| 35 | + |
| 36 | + /** |
| 37 | + * Global instance of the scopes required by this quickstart. |
| 38 | + * |
| 39 | + * <p>If modifying these scopes, delete your previously saved credentials at |
| 40 | + * ~/.credentials/driveactivity-java-quickstart |
| 41 | + */ |
| 42 | + private static final List<String> SCOPES = |
| 43 | + Arrays.asList(DriveActivityScopes.DRIVE_ACTIVITY_READONLY); |
| 44 | + |
| 45 | + static { |
| 46 | + try { |
| 47 | + HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
| 48 | + DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); |
| 49 | + } catch (Throwable t) { |
| 50 | + t.printStackTrace(); |
| 51 | + System.exit(1); |
| 52 | + } |
52 | 53 | } |
53 | | - } |
54 | | - |
55 | | - /** |
56 | | - * Creates an authorized Credential object. |
57 | | - * |
58 | | - * @return an authorized Credential object. |
59 | | - * @throws IOException |
60 | | - */ |
61 | | - public static Credential authorize() throws IOException { |
62 | | - // Load client secrets. |
63 | | - InputStream in = DriveActivityQuickstart.class.getResourceAsStream("/credentials.json"); |
64 | | - GoogleClientSecrets clientSecrets = |
65 | | - GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
66 | | - |
67 | | - // Build flow and trigger user authorization request. |
68 | | - GoogleAuthorizationCodeFlow flow = |
69 | | - new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
70 | | - .setDataStoreFactory(DATA_STORE_FACTORY) |
71 | | - .setAccessType("offline") |
72 | | - .build(); |
73 | | - Credential credential = |
74 | | - new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); |
75 | | - System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); |
76 | | - return credential; |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Build and return an authorized Drive Activity client service. |
81 | | - * |
82 | | - * @return an authorized DriveActivity client service |
83 | | - * @throws IOException |
84 | | - */ |
85 | | - public static com.google.api.services.driveactivity.v2.DriveActivity getDriveActivityService() |
86 | | - throws IOException { |
87 | | - Credential credential = authorize(); |
88 | | - return new com.google.api.services.driveactivity.v2.DriveActivity.Builder( |
89 | | - HTTP_TRANSPORT, JSON_FACTORY, credential) |
90 | | - .setApplicationName(APPLICATION_NAME) |
91 | | - .build(); |
92 | | - } |
93 | | - |
94 | | - public static void main(String[] args) throws IOException { |
95 | | - // Build a new authorized API client service. |
96 | | - com.google.api.services.driveactivity.v2.DriveActivity service = getDriveActivityService(); |
97 | | - |
98 | | - // Print the recent activity in your Google Drive. |
99 | | - QueryDriveActivityResponse result = |
100 | | - service |
101 | | - .activity() |
102 | | - .query( |
103 | | - new QueryDriveActivityRequest() |
104 | | - .setPageSize(10) |
105 | | - .setConsolidationStrategy(new ConsolidationStrategy().setLegacy(new Legacy()))) |
106 | | - .execute(); |
107 | | - List<DriveActivity> activities = result.getActivities(); |
108 | | - if (activities == null || activities.size() == 0) { |
109 | | - System.out.println("No activity."); |
110 | | - } else { |
111 | | - System.out.println("Recent activity:"); |
112 | | - for (DriveActivity activity : activities) { |
113 | | - String time = getTimeInfo(activity); |
114 | | - String action = getActionInfo(activity.getPrimaryActionDetail()); |
115 | | - List<String> actors = |
116 | | - activity.getActors().stream() |
117 | | - .map(DriveActivityQuickstart::getActorInfo) |
118 | | - .collect(Collectors.toList()); |
119 | | - List<String> targets = |
120 | | - activity.getTargets().stream() |
121 | | - .map(DriveActivityQuickstart::getTargetInfo) |
122 | | - .collect(Collectors.toList()); |
123 | | - System.out.printf("%s: %s, %s, %s\n", time, truncated(actors), action, truncated(targets)); |
124 | | - } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates an authorized Credential object. |
| 57 | + * |
| 58 | + * @return an authorized Credential object. |
| 59 | + * @throws IOException |
| 60 | + */ |
| 61 | + public static Credential authorize() throws IOException { |
| 62 | + // Load client secrets. |
| 63 | + InputStream in = DriveActivityQuickstart.class.getResourceAsStream("/credentials.json"); |
| 64 | + GoogleClientSecrets clientSecrets = |
| 65 | + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
| 66 | + |
| 67 | + // Build flow and trigger user authorization request. |
| 68 | + GoogleAuthorizationCodeFlow flow = |
| 69 | + new GoogleAuthorizationCodeFlow.Builder( |
| 70 | + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
| 71 | + .setDataStoreFactory(DATA_STORE_FACTORY) |
| 72 | + .setAccessType("offline") |
| 73 | + .build(); |
| 74 | + Credential credential = |
| 75 | + new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) |
| 76 | + .authorize("user"); |
| 77 | + System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); |
| 78 | + return credential; |
125 | 79 | } |
126 | | - } |
127 | 80 |
|
128 | | - private static String truncated(List<String> array) { |
129 | | - return truncated(array, 2); |
130 | | - } |
| 81 | + /** |
| 82 | + * Build and return an authorized Drive Activity client service. |
| 83 | + * |
| 84 | + * @return an authorized DriveActivity client service |
| 85 | + * @throws IOException |
| 86 | + */ |
| 87 | + public static com.google.api.services.driveactivity.v2.DriveActivity getDriveActivityService() |
| 88 | + throws IOException { |
| 89 | + Credential credential = authorize(); |
| 90 | + return new com.google.api.services.driveactivity.v2.DriveActivity.Builder( |
| 91 | + HTTP_TRANSPORT, JSON_FACTORY, credential) |
| 92 | + .setApplicationName(APPLICATION_NAME) |
| 93 | + .build(); |
| 94 | + } |
131 | 95 |
|
132 | | - private static String truncated(List<String> array, int limit) { |
133 | | - String contents = array.stream().limit(limit).collect(Collectors.joining(", ")); |
134 | | - String more = array.size() > limit ? ", ..." : ""; |
135 | | - return "[" + contents + more + "]"; |
136 | | - } |
| 96 | + public static void main(String[] args) throws IOException { |
| 97 | + // Build a new authorized API client service. |
| 98 | + com.google.api.services.driveactivity.v2.DriveActivity service = getDriveActivityService(); |
| 99 | + |
| 100 | + // Print the recent activity in your Google Drive. |
| 101 | + QueryDriveActivityResponse result = |
| 102 | + service.activity().query(new QueryDriveActivityRequest().setPageSize(10)).execute(); |
| 103 | + List<DriveActivity> activities = result.getActivities(); |
| 104 | + if (activities == null || activities.size() == 0) { |
| 105 | + System.out.println("No activity."); |
| 106 | + } else { |
| 107 | + System.out.println("Recent activity:"); |
| 108 | + for (DriveActivity activity : activities) { |
| 109 | + String time = getTimeInfo(activity); |
| 110 | + String action = getActionInfo(activity.getPrimaryActionDetail()); |
| 111 | + List<String> actors = |
| 112 | + activity.getActors().stream() |
| 113 | + .map(DriveActivityQuickstart::getActorInfo) |
| 114 | + .collect(Collectors.toList()); |
| 115 | + List<String> targets = |
| 116 | + activity.getTargets().stream() |
| 117 | + .map(DriveActivityQuickstart::getTargetInfo) |
| 118 | + .collect(Collectors.toList()); |
| 119 | + System.out.printf( |
| 120 | + "%s: %s, %s, %s\n", time, truncated(actors), action, truncated(targets)); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
137 | 124 |
|
138 | | - private static String getTimeInfo(DriveActivity activity) { |
139 | | - if (activity.getTimestamp() != null) { |
140 | | - return activity.getTimestamp(); |
| 125 | + private static String truncated(List<String> array) { |
| 126 | + return truncated(array, 2); |
141 | 127 | } |
142 | | - if (activity.getTimeRange() != null) { |
143 | | - return activity.getTimeRange().getEndTime(); |
| 128 | + |
| 129 | + private static String truncated(List<String> array, int limit) { |
| 130 | + String contents = array.stream().limit(limit).collect(Collectors.joining(", ")); |
| 131 | + String more = array.size() > limit ? ", ..." : ""; |
| 132 | + return "[" + contents + more + "]"; |
144 | 133 | } |
145 | | - return "unknown"; |
146 | | - } |
147 | | - |
148 | | - private static String getActionInfo(ActionDetail actionDetail) { |
149 | | - return actionDetail.keySet().iterator().next(); |
150 | | - } |
151 | | - |
152 | | - private static String getUserInfo(User user) { |
153 | | - if (user.getKnownUser() != null) { |
154 | | - KnownUser knownUser = user.getKnownUser(); |
155 | | - Boolean isMe = knownUser.getIsCurrentUser(); |
156 | | - return (isMe != null && isMe) ? "people/me" : knownUser.getPersonName(); |
| 134 | + |
| 135 | + private static String getTimeInfo(DriveActivity activity) { |
| 136 | + if (activity.getTimestamp() != null) { |
| 137 | + return activity.getTimestamp(); |
| 138 | + } |
| 139 | + if (activity.getTimeRange() != null) { |
| 140 | + return activity.getTimeRange().getEndTime(); |
| 141 | + } |
| 142 | + return "unknown"; |
157 | 143 | } |
158 | | - return user.keySet().iterator().next(); |
159 | | - } |
160 | 144 |
|
161 | | - private static String getActorInfo(Actor actor) { |
162 | | - if (actor.getUser() != null) { |
163 | | - return getUserInfo(actor.getUser()); |
| 145 | + private static String getActionInfo(ActionDetail actionDetail) { |
| 146 | + return actionDetail.keySet().iterator().next(); |
164 | 147 | } |
165 | | - return actor.keySet().iterator().next(); |
166 | | - } |
167 | 148 |
|
168 | | - private static String getTargetInfo(Target target) { |
169 | | - if (target.getDriveItem() != null) { |
170 | | - return "driveItem:\"" + target.getDriveItem().getTitle() + "\""; |
| 149 | + private static String getUserInfo(User user) { |
| 150 | + if (user.getKnownUser() != null) { |
| 151 | + KnownUser knownUser = user.getKnownUser(); |
| 152 | + Boolean isMe = knownUser.getIsCurrentUser(); |
| 153 | + return (isMe != null && isMe) ? "people/me" : knownUser.getPersonName(); |
| 154 | + } |
| 155 | + return user.keySet().iterator().next(); |
171 | 156 | } |
172 | | - if (target.getTeamDrive() != null) { |
173 | | - return "teamDrive:\"" + target.getTeamDrive().getTitle() + "\""; |
| 157 | + |
| 158 | + private static String getActorInfo(Actor actor) { |
| 159 | + if (actor.getUser() != null) { |
| 160 | + return getUserInfo(actor.getUser()); |
| 161 | + } |
| 162 | + return actor.keySet().iterator().next(); |
174 | 163 | } |
175 | | - if (target.getFileComment() != null) { |
176 | | - DriveItem parent = target.getFileComment().getParent(); |
177 | | - if (parent != null) { |
178 | | - return "fileComment:\"" + parent.getTitle() + "\""; |
179 | | - } |
180 | | - return "fileComment:unknown"; |
| 164 | + |
| 165 | + private static String getTargetInfo(Target target) { |
| 166 | + if (target.getDriveItem() != null) { |
| 167 | + return "driveItem:\"" + target.getDriveItem().getTitle() + "\""; |
| 168 | + } |
| 169 | + if (target.getTeamDrive() != null) { |
| 170 | + return "teamDrive:\"" + target.getTeamDrive().getTitle() + "\""; |
| 171 | + } |
| 172 | + if (target.getFileComment() != null) { |
| 173 | + DriveItem parent = target.getFileComment().getParent(); |
| 174 | + if (parent != null) { |
| 175 | + return "fileComment:\"" + parent.getTitle() + "\""; |
| 176 | + } |
| 177 | + return "fileComment:unknown"; |
| 178 | + } |
| 179 | + return target.keySet().iterator().next(); |
181 | 180 | } |
182 | | - return target.keySet().iterator().next(); |
183 | | - } |
184 | 181 | } |
0 commit comments