Skip to content

Commit d296724

Browse files
author
Eric Koleda
authored
Merge pull request googleworkspace#45 from jsmeredith/master
Adding java quickstart for Drive Activity v2 API.
2 parents 939cfbb + 3c696c8 commit d296724

4 files changed

Lines changed: 233 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: 'java'
2+
apply plugin: 'application'
3+
4+
mainClassName = 'DriveActivityQuickstart'
5+
sourceCompatibility = 1.8
6+
targetCompatibility = 1.8
7+
version = '1.0'
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
compile 'com.google.api-client:google-api-client:1.27.0'
15+
compile 'com.google.oauth-client:google-oauth-client-jetty:1.27.0'
16+
compile 'com.google.apis:google-api-services-driveactivity:v2-rev20181113-1.27.0'
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* This settings file was generated by the Gradle 'init' task.
3+
*
4+
* The settings file is used to specify which projects to include in your build.
5+
* In a single project build this file can be empty or even removed.
6+
*
7+
* Detailed information about configuring a multi-project build in Gradle can be found
8+
* in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html
9+
*/
10+
11+
/*
12+
// To declare projects as part of a multi-project build use the 'include' method
13+
include 'shared'
14+
include 'api'
15+
include 'services:webservice'
16+
*/
17+
18+
rootProject.name = 'quickstart'
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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_v2_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.JsonFactory;
24+
import com.google.api.client.json.jackson2.JacksonFactory;
25+
import com.google.api.client.util.store.FileDataStoreFactory;
26+
import com.google.api.services.driveactivity.v2.DriveActivityScopes;
27+
import com.google.api.services.driveactivity.v2.model.*;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.InputStreamReader;
31+
import java.util.Arrays;
32+
import java.util.List;
33+
import java.util.stream.Collectors;
34+
35+
public class DriveActivityQuickstart {
36+
/** Application name. */
37+
private static final String APPLICATION_NAME = "Drive Activity API Java Quickstart";
38+
39+
/** Directory to store authorization tokens for this application. */
40+
private static final java.io.File DATA_STORE_DIR = new java.io.File("tokens");
41+
42+
/** Global instance of the {@link FileDataStoreFactory}. */
43+
private static FileDataStoreFactory DATA_STORE_FACTORY;
44+
45+
/** Global instance of the JSON factory. */
46+
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
47+
48+
/** Global instance of the HTTP transport. */
49+
private static HttpTransport HTTP_TRANSPORT;
50+
51+
/**
52+
* Global instance of the scopes required by this quickstart.
53+
*
54+
* <p>If modifying these scopes, delete your previously saved credentials at
55+
* ~/.credentials/driveactivity-java-quickstart
56+
*/
57+
private static final List<String> SCOPES =
58+
Arrays.asList(DriveActivityScopes.DRIVE_ACTIVITY_READONLY);
59+
60+
static {
61+
try {
62+
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
63+
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
64+
} catch (Throwable t) {
65+
t.printStackTrace();
66+
System.exit(1);
67+
}
68+
}
69+
70+
/**
71+
* Creates an authorized Credential object.
72+
*
73+
* @return an authorized Credential object.
74+
* @throws IOException
75+
*/
76+
public static Credential authorize() throws IOException {
77+
// Load client secrets.
78+
InputStream in = DriveActivityQuickstart.class.getResourceAsStream("/credentials.json");
79+
GoogleClientSecrets clientSecrets =
80+
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
81+
82+
// Build flow and trigger user authorization request.
83+
GoogleAuthorizationCodeFlow flow =
84+
new GoogleAuthorizationCodeFlow.Builder(
85+
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
86+
.setDataStoreFactory(DATA_STORE_FACTORY)
87+
.setAccessType("offline")
88+
.build();
89+
Credential credential =
90+
new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
91+
.authorize("user");
92+
System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
93+
return credential;
94+
}
95+
96+
/**
97+
* Build and return an authorized Drive Activity client service.
98+
*
99+
* @return an authorized DriveActivity client service
100+
* @throws IOException
101+
*/
102+
public static com.google.api.services.driveactivity.v2.DriveActivity getDriveActivityService()
103+
throws IOException {
104+
Credential credential = authorize();
105+
return new com.google.api.services.driveactivity.v2.DriveActivity.Builder(
106+
HTTP_TRANSPORT, JSON_FACTORY, credential)
107+
.setApplicationName(APPLICATION_NAME)
108+
.build();
109+
}
110+
111+
public static void main(String[] args) throws IOException {
112+
// Build a new authorized API client service.
113+
com.google.api.services.driveactivity.v2.DriveActivity service = getDriveActivityService();
114+
115+
// Print the recent activity in your Google Drive.
116+
QueryDriveActivityResponse result =
117+
service.activity().query(new QueryDriveActivityRequest().setPageSize(10)).execute();
118+
List<DriveActivity> activities = result.getActivities();
119+
if (activities == null || activities.size() == 0) {
120+
System.out.println("No activity.");
121+
} else {
122+
System.out.println("Recent activity:");
123+
for (DriveActivity activity : activities) {
124+
String time = getTimeInfo(activity);
125+
String action = getActionInfo(activity.getPrimaryActionDetail());
126+
List<String> actors =
127+
activity.getActors().stream()
128+
.map(DriveActivityQuickstart::getActorInfo)
129+
.collect(Collectors.toList());
130+
List<String> targets =
131+
activity.getTargets().stream()
132+
.map(DriveActivityQuickstart::getTargetInfo)
133+
.collect(Collectors.toList());
134+
System.out.printf(
135+
"%s: %s, %s, %s\n", time, truncated(actors), action, truncated(targets));
136+
}
137+
}
138+
}
139+
140+
private static String truncated(List<String> array) {
141+
return truncated(array, 2);
142+
}
143+
144+
private static String truncated(List<String> array, int limit) {
145+
String contents = array.stream().limit(limit).collect(Collectors.joining(", "));
146+
String more = array.size() > limit ? ", ..." : "";
147+
return "[" + contents + more + "]";
148+
}
149+
150+
private static String getTimeInfo(DriveActivity activity) {
151+
if (activity.getTimestamp() != null) {
152+
return activity.getTimestamp();
153+
}
154+
if (activity.getTimeRange() != null) {
155+
return activity.getTimeRange().getEndTime();
156+
}
157+
return "unknown";
158+
}
159+
160+
private static String getActionInfo(ActionDetail actionDetail) {
161+
return actionDetail.keySet().iterator().next();
162+
}
163+
164+
private static String getUserInfo(User user) {
165+
if (user.getKnownUser() != null) {
166+
KnownUser knownUser = user.getKnownUser();
167+
Boolean isMe = knownUser.getIsCurrentUser();
168+
return (isMe != null && isMe) ? "people/me" : knownUser.getPersonName();
169+
}
170+
return user.keySet().iterator().next();
171+
}
172+
173+
private static String getActorInfo(Actor actor) {
174+
if (actor.getUser() != null) {
175+
return getUserInfo(actor.getUser());
176+
}
177+
return actor.keySet().iterator().next();
178+
}
179+
180+
private static String getTargetInfo(Target target) {
181+
if (target.getDriveItem() != null) {
182+
return "driveItem:\"" + target.getDriveItem().getTitle() + "\"";
183+
}
184+
if (target.getTeamDrive() != null) {
185+
return "teamDrive:\"" + target.getTeamDrive().getTitle() + "\"";
186+
}
187+
if (target.getFileComment() != null) {
188+
DriveItem parent = target.getFileComment().getParent();
189+
if (parent != null) {
190+
return "fileComment:\"" + parent.getTitle() + "\"";
191+
}
192+
return "fileComment:unknown";
193+
}
194+
return target.keySet().iterator().next();
195+
}
196+
}
197+
// [END drive_activity_v2_quickstart]

drive/activity/quickstart/src/main/java/DriveActivityQuickstart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public class DriveActivityQuickstart {
3939
/** Application name. */
4040
private static final String APPLICATION_NAME =
41-
"G Suite Activity API Java Quickstart";
41+
"Drive Activity API Java Quickstart";
4242

4343
/** Directory to store authorization tokens for this application. */
4444
private static final java.io.File DATA_STORE_DIR = new java.io.File("tokens");

0 commit comments

Comments
 (0)