-
Notifications
You must be signed in to change notification settings - Fork 410
Add Java quickstart for Alert Center API #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
erickoledadevrel
merged 4 commits into
googleworkspace:master
from
aozarov:aozarov-patch-2
Nov 15, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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' | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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' |
112 changes: 112 additions & 0 deletions
112
adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String> 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(); | ||
|
aozarov marked this conversation as resolved.
|
||
| System.out.println(feedback); | ||
| // [END admin_sdk_alertcenter_provide_feedback] | ||
| } | ||
| } | ||
| // [END admin_sdk_alertcenter_quickstart] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.