Java quickstart

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

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.

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

  1. In the Google Cloud console, go to Menu>IAM & Admin>Service Accounts.

    Go to Service Accounts

  2. ClickCreate service account.
  3. 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.
  4. 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.
  5. ClickContinue.
  6. Optional: Enter users or groups that can manage and perform actions with this service account. For more details, refer toManaging service account impersonation.
  7. ClickDone. Make a note of the email address for the service account.

gcloud CLI

  1. Create the service account:
    gcloud iam service-accounts createSERVICE_ACCOUNT_NAME \  --display-name="SERVICE_ACCOUNT_NAME"
  2. 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.
  1. In the Google Cloud console, go to Menu>IAM & Admin>Service Accounts.

    Go to Service Accounts

  2. Select your service account.
  3. ClickKeys>Add key>Create new key.
  4. 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 ascredentials.json in 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.

  5. ClickClose.

Set up and run the sample

  1. Get credentials:

    adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java
    InputStreamin=AdminSDKAlertCenterQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);if(in==null){thrownewFileNotFoundException("Resource not found: "+CREDENTIALS_FILE_PATH);}GoogleCredentialscredentials=ServiceAccountCredentials.fromStream(in).createDelegated(delegatedAdminEmail).createScoped(SCOPES);
  2. Create an instance of the client:

    adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java
    NetHttpTransporttransport=GoogleNetHttpTransport.newTrustedTransport();AlertCenterservice=newAlertCenter.Builder(transport,JSON_FACTORY,newHttpCredentialsAdapter(getCredentials(DELEGATED_ADMIN_EMAIL))).setApplicationName(APPLICATION_NAME).build();
  3. Display all alerts:

    adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java
    StringpageToken=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);
  4. Provide feedback on a given alert:

    adminSDK/alertcenter/quickstart/src/main/java/AdminSDKAlertCenterQuickstart.java
    AlertFeedbacknewFeedback=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.