Java quickstart Stay organized with collections Save and categorize content based on your preferences.
Quickstarts explain how to set up and run an app that calls aGoogle Workspace API. This quickstart uses asimplified authentication approach that is appropriate for a testingenvironment. For a production environment, we recommend learning aboutauthentication and authorizationbeforechoosing the access credentialsthat are appropriate for your app.
As a Google Workspace administrator, you can use the Alert Center API to accessalerts for issues affecting your domain.
Objectives
- Set up your environment.
- Set up and run the sample.
Prerequisites
- Java 1.8 or greater
- A Google Cloud project
- A Google Account
Set up your environment
Turn on the API
Before using Google APIs, you need to turn them on in a Google Cloud project.You can turn on one or more APIs in a single Google Cloud project.In the Google Cloud console, turn on the Alert Center API.
Create a service account
A service account is a special kind of account used by an application, ratherthan a person. You can use a service account to access data or perform actionsby the robot account, or to access data on behalf of Google Workspaceor Cloud Identity users. For more information, seeUnderstanding service accounts.Google Cloud console
- In the Google Cloud console, go to Menu>IAM & Admin>Service Accounts.
- ClickCreate service account.
- Fill in the service account details, then clickCreate and continue.Note: By default, Google creates a unique service account ID. If you would like to change the ID, modify the ID in the service account ID field.
- Optional: Assign roles to your service account to grant access to your Google Cloud project's resources. For more details, refer toGranting, changing, and revoking access to resources.
- ClickContinue.
- Optional: Enter users or groups that can manage and perform actions with this service account. For more details, refer toManaging service account impersonation.
- ClickDone. Make a note of the email address for the service account.
gcloud CLI
- Create the service account:
gcloud iam service-accounts createSERVICE_ACCOUNT_NAME\ --display-name="SERVICE_ACCOUNT_NAME" - Optional: Assign roles to your service account to grant access to your Google Cloud project's resources. For more details, refer toGranting, changing, and revoking access to resources.
Create credentials for a service account
You need to obtain credentials in the form of a public/private key pair. Thesecredentials are used by your code to authorize service account actions withinyour app.- In the Google Cloud console, go to Menu>IAM & Admin>Service Accounts.
- Select your service account.
- ClickKeys>Add key>Create new key.
- SelectJSON, then clickCreate.
Your new public/private key pair is generated and downloaded to your machine as a new file. Save the downloaded JSON file as
credentials.jsonin your working directory. This file is the only copy of this key. For information about how to store your key securely, seeManaging service account keys. - ClickClose.
Set up and run the sample
Get credentials:
adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.javaInputStreamin=AdminSDKAlertCenterQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);if(in==null){thrownewFileNotFoundException("Resource not found: "+CREDENTIALS_FILE_PATH);}GoogleCredentialscredentials=ServiceAccountCredentials.fromStream(in).createDelegated(delegatedAdminEmail).createScoped(SCOPES);
Create an instance of the client:
adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.javaNetHttpTransporttransport=GoogleNetHttpTransport.newTrustedTransport();AlertCenterservice=newAlertCenter.Builder(transport,JSON_FACTORY,newHttpCredentialsAdapter(getCredentials(DELEGATED_ADMIN_EMAIL))).setApplicationName(APPLICATION_NAME).build();
Display all alerts:
adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.javaStringpageToken=null;do{ListAlertsResponselistResponse=service.alerts().list().setPageToken(pageToken).setPageSize(20).execute();if(listResponse.getAlerts()!=null){for(Alertalert:listResponse.getAlerts()){System.out.println(alert);}}pageToken=listResponse.getNextPageToken();}while(pageToken!=null);
Provide feedback on a given alert:
adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.javaAlertFeedbacknewFeedback=newAlertFeedback();newFeedback.setType("VERY_USEFUL");AlertFeedbackfeedback=service.alerts().feedback().create(alertId,newFeedback).execute();System.out.println(feedback);
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-11 UTC.