diff --git a/.travis.yml b/.travis.yml index 899860b0..ca28a01d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ jdk: # - oraclejdk9 # Use a Build Matrix for subdirs (https://lord.io/blog/2014/travis-multiple-subdirs/) env: + - DIR=adminSDK/alertcenter/quickstart - DIR=adminSDK/directory/quickstart - DIR=adminSDK/reports/quickstart - DIR=adminSDK/reseller/quickstart diff --git a/README.md b/README.md index 888e3088..453cfcb2 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ A collection of samples that demonstrate how to call G Suite APIs in Java. ### Admin SDK +- [Alert Center Quickstart](https://developers.google.com/admin-sdk/alertcenter/quickstart/java) - [Directory Quickstart](https://developers.google.com/admin-sdk/directory/v1/quickstart/java) - [Reports Quickstart](https://developers.google.com/admin-sdk/reports/v1/quickstart/java) - [Reseller Quickstart](https://developers.google.com/admin-sdk/reseller/v1/quickstart/java) diff --git a/adminSDK/alertcenter/quickstart/build.gradle b/adminSDK/alertcenter/quickstart/build.gradle new file mode 100644 index 00000000..25d08517 --- /dev/null +++ b/adminSDK/alertcenter/quickstart/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'java' +apply plugin: 'application' +apply plugin: 'idea' + +mainClassName = 'AdminSDKAlertCenterQuickstart' +sourceCompatibility = 1.8 +targetCompatibility = 1.8 +version = '1.0' + +repositories { + mavenCentral() +} + +dependencies { + compile 'com.google.api-client:google-api-client:1.20.0' + compile 'com.google.auth:google-auth-library-oauth2-http:0.11.0' + compile 'com.google.apis:google-api-services-alertcenter:v1beta1-rev7-1.25.0' +} \ No newline at end of file diff --git a/adminSDK/alertcenter/quickstart/settings.gradle b/adminSDK/alertcenter/quickstart/settings.gradle new file mode 100644 index 00000000..7bcc727d --- /dev/null +++ b/adminSDK/alertcenter/quickstart/settings.gradle @@ -0,0 +1,18 @@ +/* + * This settings file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * In a single project build this file can be empty or even removed. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html + */ + +/* +// To declare projects as part of a multi-project build use the 'include' method +include 'shared' +include 'api' +include 'services:webservice' +*/ + +rootProject.name = 'quickstart' diff --git a/adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java b/adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java new file mode 100644 index 00000000..cfca6b29 --- /dev/null +++ b/adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java @@ -0,0 +1,112 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// [START admin_sdk_alertcenter_quickstart] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.JsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.services.alertcenter.v1beta1.AlertCenter; +import com.google.api.services.alertcenter.v1beta1.model.Alert; +import com.google.api.services.alertcenter.v1beta1.model.AlertFeedback; +import com.google.api.services.alertcenter.v1beta1.model.ListAlertsResponse; +import com.google.auth.Credentials; +import com.google.auth.http.HttpCredentialsAdapter; +import com.google.auth.oauth2.GoogleCredentials; +import com.google.auth.oauth2.ServiceAccountCredentials; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.util.Collections; +import java.util.List; + +public class AdminSDKAlertCenterQuickstart { + + private static final String APPLICATION_NAME = "Google Admin SDK Alert Center API Java Quickstart"; + private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); + /** + * Global instance of the scopes required by this quickstart. If modifying these scopes, delete + * your previously saved tokens/ folder. + */ + private static final List SCOPES = Collections + .singletonList("https://www.googleapis.com/auth/apps.alerts"); + private static final String CREDENTIALS_FILE_PATH = "/credentials.json"; + private static final String DELEGATED_ADMIN_EMAIL = "admin@xxx.com"; + + /** + * Creates an authorized Credentials object. + * + * @param delegatedAdminEmail A delegated admin email to associate with the created credentials. + * @return An authorized Credentials object. + * @throws IOException If the credentials.json file cannot be found. + */ + private static Credentials getCredentials(String delegatedAdminEmail) throws IOException { + // [START admin_sdk_alertcenter_get_credentials] + InputStream in = AdminSDKAlertCenterQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH); + if (in == null) { + throw new IOException("Credential file was not found"); + } + GoogleCredentials credentials = ServiceAccountCredentials + .fromStream(in) + .createDelegated(delegatedAdminEmail) + .createScoped(SCOPES); + // [END admin_sdk_alertcenter_get_credentials] + return credentials; + } + + public static void main(String... args) throws IOException, GeneralSecurityException { + // [START admin_sdk_alertcenter_create_client] + + NetHttpTransport transport = GoogleNetHttpTransport.newTrustedTransport(); + AlertCenter service = new AlertCenter.Builder(transport, JSON_FACTORY, + new HttpCredentialsAdapter(getCredentials(DELEGATED_ADMIN_EMAIL))) + .setApplicationName(APPLICATION_NAME) + .build(); + // [END admin_sdk_alertcenter_create_client] + + // [START admin_sdk_alertcenter_list_alerts] + String pageToken = null; + do { + ListAlertsResponse listResponse = service.alerts().list().setPageToken(pageToken) + .setPageSize(20).execute(); + if (listResponse.getAlerts() != null) { + for (Alert alert : listResponse.getAlerts()) { + System.out.println(alert); + } + } + pageToken = listResponse.getNextPageToken(); + } while (pageToken != null); + // [END admin_sdk_alertcenter_list_alerts] + + ListAlertsResponse listResponse = service.alerts().list().setPageSize(20).execute(); + if (listResponse == null || listResponse.isEmpty()) { + System.out.println("No alerts"); + } else { + String alertId = listResponse.getAlerts().get(0).getAlertId(); + // Uncomment the line below to set alert feedback. + // setAlertFeedback(service, alertId); + } + } + + private static void setAlertFeedback(AlertCenter service, String alertId) throws IOException { + // [START admin_sdk_alertcenter_provide_feedback] + AlertFeedback newFeedback = new AlertFeedback(); + newFeedback.setType("VERY_USEFUL"); + AlertFeedback feedback = service.alerts().feedback().create(alertId, newFeedback).execute(); + System.out.println(feedback); + // [END admin_sdk_alertcenter_provide_feedback] + } +} +// [END admin_sdk_alertcenter_quickstart]