|
| 1 | +import com.google.api.client.auth.oauth2.Credential; |
| 2 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 3 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 4 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 5 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 6 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 7 | +import com.google.api.client.http.HttpTransport; |
| 8 | +import com.google.api.client.json.JsonFactory; |
| 9 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 10 | +import com.google.api.client.util.store.FileDataStoreFactory; |
| 11 | +import com.google.api.services.driveactivity.v2.DriveActivityScopes; |
| 12 | +import com.google.api.services.driveactivity.v2.model.*; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 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); |
| 52 | + } |
| 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 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static String truncated(List<String> array) { |
| 129 | + return truncated(array, 2); |
| 130 | + } |
| 131 | + |
| 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 | + } |
| 137 | + |
| 138 | + private static String getTimeInfo(DriveActivity activity) { |
| 139 | + if (activity.getTimestamp() != null) { |
| 140 | + return activity.getTimestamp(); |
| 141 | + } |
| 142 | + if (activity.getTimeRange() != null) { |
| 143 | + return activity.getTimeRange().getEndTime(); |
| 144 | + } |
| 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(); |
| 157 | + } |
| 158 | + return user.keySet().iterator().next(); |
| 159 | + } |
| 160 | + |
| 161 | + private static String getActorInfo(Actor actor) { |
| 162 | + if (actor.getUser() != null) { |
| 163 | + return getUserInfo(actor.getUser()); |
| 164 | + } |
| 165 | + return actor.keySet().iterator().next(); |
| 166 | + } |
| 167 | + |
| 168 | + private static String getTargetInfo(Target target) { |
| 169 | + if (target.getDriveItem() != null) { |
| 170 | + return "driveItem:\"" + target.getDriveItem().getTitle() + "\""; |
| 171 | + } |
| 172 | + if (target.getTeamDrive() != null) { |
| 173 | + return "teamDrive:\"" + target.getTeamDrive().getTitle() + "\""; |
| 174 | + } |
| 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"; |
| 181 | + } |
| 182 | + return target.keySet().iterator().next(); |
| 183 | + } |
| 184 | +} |
0 commit comments