|
| 1 | +// Copyright 2018 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START drive_activity_quickstart] |
| 16 | +import com.google.api.client.auth.oauth2.Credential; |
| 17 | +import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
| 18 | +import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
| 19 | +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
| 20 | +import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
| 21 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 22 | +import com.google.api.client.http.HttpTransport; |
| 23 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 24 | +import com.google.api.client.json.JsonFactory; |
| 25 | +import com.google.api.client.util.store.FileDataStoreFactory; |
| 26 | + |
| 27 | +import com.google.api.services.appsactivity.AppsactivityScopes; |
| 28 | +import com.google.api.services.appsactivity.model.*; |
| 29 | +import com.google.api.services.appsactivity.Appsactivity; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.io.InputStreamReader; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | + |
| 38 | +public class DriveActivityQuickstart { |
| 39 | + /** Application name. */ |
| 40 | + private static final String APPLICATION_NAME = |
| 41 | + "G Suite Activity API Java Quickstart"; |
| 42 | + |
| 43 | + /** Directory to store user credentials for this application. */ |
| 44 | + private static final java.io.File DATA_STORE_DIR = new java.io.File( |
| 45 | + System.getProperty("user.home"), ".credentials/appsactivity-java-quickstart"); |
| 46 | + |
| 47 | + /** Global instance of the {@link FileDataStoreFactory}. */ |
| 48 | + private static FileDataStoreFactory DATA_STORE_FACTORY; |
| 49 | + |
| 50 | + /** Global instance of the JSON factory. */ |
| 51 | + private static final JsonFactory JSON_FACTORY = |
| 52 | + JacksonFactory.getDefaultInstance(); |
| 53 | + |
| 54 | + /** Global instance of the HTTP transport. */ |
| 55 | + private static HttpTransport HTTP_TRANSPORT; |
| 56 | + |
| 57 | + /** Global instance of the scopes required by this quickstart. |
| 58 | + * |
| 59 | + * If modifying these scopes, delete your previously saved credentials |
| 60 | + * at ~/.credentials/appsactivity-java-quickstart |
| 61 | + */ |
| 62 | + private static final List<String> SCOPES = |
| 63 | + Arrays.asList(AppsactivityScopes.ACTIVITY, |
| 64 | + AppsactivityScopes.DRIVE_METADATA_READONLY); |
| 65 | + |
| 66 | + static { |
| 67 | + try { |
| 68 | + HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
| 69 | + DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); |
| 70 | + } catch (Throwable t) { |
| 71 | + t.printStackTrace(); |
| 72 | + System.exit(1); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Creates an authorized Credential object. |
| 78 | + * @return an authorized Credential object. |
| 79 | + * @throws IOException |
| 80 | + */ |
| 81 | + public static Credential authorize() throws IOException { |
| 82 | + // Load client secrets. |
| 83 | + InputStream in = |
| 84 | + DriveActivityQuickstart.class.getResourceAsStream("/client_secret.json"); |
| 85 | + GoogleClientSecrets clientSecrets = |
| 86 | + GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
| 87 | + |
| 88 | + // Build flow and trigger user authorization request. |
| 89 | + GoogleAuthorizationCodeFlow flow = |
| 90 | + new GoogleAuthorizationCodeFlow.Builder( |
| 91 | + HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
| 92 | + .setDataStoreFactory(DATA_STORE_FACTORY) |
| 93 | + .setAccessType("offline") |
| 94 | + .build(); |
| 95 | + Credential credential = new AuthorizationCodeInstalledApp( |
| 96 | + flow, new LocalServerReceiver()).authorize("user"); |
| 97 | + System.out.println( |
| 98 | + "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); |
| 99 | + return credential; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Build and return an authorized Apps Activity client service. |
| 104 | + * @return an authorized Appsactivity client service |
| 105 | + * @throws IOException |
| 106 | + */ |
| 107 | + public static Appsactivity getAppsactivityService() throws IOException { |
| 108 | + Credential credential = authorize(); |
| 109 | + return new Appsactivity.Builder( |
| 110 | + HTTP_TRANSPORT, JSON_FACTORY, credential) |
| 111 | + .setApplicationName(APPLICATION_NAME) |
| 112 | + .build(); |
| 113 | + } |
| 114 | + |
| 115 | + public static void main(String[] args) throws IOException { |
| 116 | + // Build a new authorized API client service. |
| 117 | + Appsactivity service = getAppsactivityService(); |
| 118 | + |
| 119 | + // Print the recent activity in your Google Drive. |
| 120 | + ListActivitiesResponse result = service.activities().list() |
| 121 | + .setSource("drive.google.com") |
| 122 | + .setDriveAncestorId("root") |
| 123 | + .setPageSize(10) |
| 124 | + .execute(); |
| 125 | + List<Activity> activities = result.getActivities(); |
| 126 | + if (activities == null || activities.size() == 0) { |
| 127 | + System.out.println("No activity."); |
| 128 | + } else { |
| 129 | + System.out.println("Recent activity:"); |
| 130 | + for (Activity activity : activities) { |
| 131 | + Event event = activity.getCombinedEvent(); |
| 132 | + User user = event.getUser(); |
| 133 | + Target target = event.getTarget(); |
| 134 | + if (user == null || target == null ) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + String date = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") |
| 138 | + .format(new java.util.Date(event.getEventTimeMillis().longValue())); |
| 139 | + System.out.printf("%s: %s, %s, %s (%s)\n", |
| 140 | + date, |
| 141 | + user.getName(), |
| 142 | + event.getPrimaryEventType(), |
| 143 | + target.getName(), |
| 144 | + target.getMimeType()); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | +// [END drive_activity_quickstart] |
0 commit comments