|
| 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