Skip to content

Commit 939cfbb

Browse files
author
Eric Koleda
authored
Merge pull request googleworkspace#43 from aozarov/aozarov-patch-2
Add Java quickstart for Alert Center API
2 parents b5698ec + 48bc7ec commit 939cfbb

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ jdk:
44
# - oraclejdk9
55
# Use a Build Matrix for subdirs (https://lord.io/blog/2014/travis-multiple-subdirs/)
66
env:
7+
- DIR=adminSDK/alertcenter/quickstart
78
- DIR=adminSDK/directory/quickstart
89
- DIR=adminSDK/reports/quickstart
910
- DIR=adminSDK/reseller/quickstart

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ A collection of samples that demonstrate how to call G Suite APIs in Java.
66

77
### Admin SDK
88

9+
- [Alert Center Quickstart](https://developers.google.com/admin-sdk/alertcenter/quickstart/java)
910
- [Directory Quickstart](https://developers.google.com/admin-sdk/directory/v1/quickstart/java)
1011
- [Reports Quickstart](https://developers.google.com/admin-sdk/reports/v1/quickstart/java)
1112
- [Reseller Quickstart](https://developers.google.com/admin-sdk/reseller/v1/quickstart/java)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: 'java'
2+
apply plugin: 'application'
3+
apply plugin: 'idea'
4+
5+
mainClassName = 'AdminSDKAlertCenterQuickstart'
6+
sourceCompatibility = 1.8
7+
targetCompatibility = 1.8
8+
version = '1.0'
9+
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
dependencies {
15+
compile 'com.google.api-client:google-api-client:1.20.0'
16+
compile 'com.google.auth:google-auth-library-oauth2-http:0.11.0'
17+
compile 'com.google.apis:google-api-services-alertcenter:v1beta1-rev7-1.25.0'
18+
}
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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 admin_sdk_alertcenter_quickstart]
16+
17+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
18+
import com.google.api.client.http.javanet.NetHttpTransport;
19+
import com.google.api.client.json.JsonFactory;
20+
import com.google.api.client.json.jackson2.JacksonFactory;
21+
import com.google.api.services.alertcenter.v1beta1.AlertCenter;
22+
import com.google.api.services.alertcenter.v1beta1.model.Alert;
23+
import com.google.api.services.alertcenter.v1beta1.model.AlertFeedback;
24+
import com.google.api.services.alertcenter.v1beta1.model.ListAlertsResponse;
25+
import com.google.auth.Credentials;
26+
import com.google.auth.http.HttpCredentialsAdapter;
27+
import com.google.auth.oauth2.GoogleCredentials;
28+
import com.google.auth.oauth2.ServiceAccountCredentials;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.security.GeneralSecurityException;
32+
import java.util.Collections;
33+
import java.util.List;
34+
35+
public class AdminSDKAlertCenterQuickstart {
36+
37+
private static final String APPLICATION_NAME = "Google Admin SDK Alert Center API Java Quickstart";
38+
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
39+
/**
40+
* Global instance of the scopes required by this quickstart. If modifying these scopes, delete
41+
* your previously saved tokens/ folder.
42+
*/
43+
private static final List<String> SCOPES = Collections
44+
.singletonList("https://www.googleapis.com/auth/apps.alerts");
45+
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
46+
private static final String DELEGATED_ADMIN_EMAIL = "admin@xxx.com";
47+
48+
/**
49+
* Creates an authorized Credentials object.
50+
*
51+
* @param delegatedAdminEmail A delegated admin email to associate with the created credentials.
52+
* @return An authorized Credentials object.
53+
* @throws IOException If the credentials.json file cannot be found.
54+
*/
55+
private static Credentials getCredentials(String delegatedAdminEmail) throws IOException {
56+
// [START admin_sdk_alertcenter_get_credentials]
57+
InputStream in = AdminSDKAlertCenterQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
58+
if (in == null) {
59+
throw new IOException("Credential file was not found");
60+
}
61+
GoogleCredentials credentials = ServiceAccountCredentials
62+
.fromStream(in)
63+
.createDelegated(delegatedAdminEmail)
64+
.createScoped(SCOPES);
65+
// [END admin_sdk_alertcenter_get_credentials]
66+
return credentials;
67+
}
68+
69+
public static void main(String... args) throws IOException, GeneralSecurityException {
70+
// [START admin_sdk_alertcenter_create_client]
71+
72+
NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
73+
AlertCenter service = new AlertCenter.Builder(transport, JSON_FACTORY,
74+
new HttpCredentialsAdapter(getCredentials(DELEGATED_ADMIN_EMAIL)))
75+
.setApplicationName(APPLICATION_NAME)
76+
.build();
77+
// [END admin_sdk_alertcenter_create_client]
78+
79+
// [START admin_sdk_alertcenter_list_alerts]
80+
String pageToken = null;
81+
do {
82+
ListAlertsResponse listResponse = service.alerts().list().setPageToken(pageToken)
83+
.setPageSize(20).execute();
84+
if (listResponse.getAlerts() != null) {
85+
for (Alert alert : listResponse.getAlerts()) {
86+
System.out.println(alert);
87+
}
88+
}
89+
pageToken = listResponse.getNextPageToken();
90+
} while (pageToken != null);
91+
// [END admin_sdk_alertcenter_list_alerts]
92+
93+
ListAlertsResponse listResponse = service.alerts().list().setPageSize(20).execute();
94+
if (listResponse == null || listResponse.isEmpty()) {
95+
System.out.println("No alerts");
96+
} else {
97+
String alertId = listResponse.getAlerts().get(0).getAlertId();
98+
// Uncomment the line below to set alert feedback.
99+
// setAlertFeedback(service, alertId);
100+
}
101+
}
102+
103+
private static void setAlertFeedback(AlertCenter service, String alertId) throws IOException {
104+
// [START admin_sdk_alertcenter_provide_feedback]
105+
AlertFeedback newFeedback = new AlertFeedback();
106+
newFeedback.setType("VERY_USEFUL");
107+
AlertFeedback feedback = service.alerts().feedback().create(alertId, newFeedback).execute();
108+
System.out.println(feedback);
109+
// [END admin_sdk_alertcenter_provide_feedback]
110+
}
111+
}
112+
// [END admin_sdk_alertcenter_quickstart]

0 commit comments

Comments
 (0)