API Requests

  • The first step in making a request using the Google APIs Client Library for Java is authentication, typically by instantiating GoogleCredentials.

  • Next, you instantiate the service class representing a version of a Google service, using a builder that requires HttpTransport, JsonFactory, and HttpRequestInitializer.

  • After instantiating the service class, you create a resource object that represents a type of resource managed by the service.

  • You then make a request object by calling methods on the resource object, which represent operations against the resource.

  • Finally, you execute the request object using the execute() method, which performs the HTTP request and deserializes the response.

Once you set up your project to declare the dependencies for your Google APIs Client Library for Java, follow these steps to make a request. The snippets in this page uses v3 of the Cloud Resource Manager API.

Step 1: Authentication

Instantiate com.google.auth.oauth2.GoogleCredentials instance. For Google Cloud users, you may use GoogleCredentials.getApplicationDefault() to get the Application Default Credentials.

GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault();

For more information on authentication, refer to Google Auth Library Java.

Step 2: Instantiate Service Class

A Google service has one or more versions. A service class represents a version of a service and is a child class of AbstractGoogleJsonClient. For example com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.

The builder class of the service class takes 3 parameters:

Also call setApplicationName() method of the builder with your application name. This sets the UserAgent header with the application name and is helpful for troubleshooting with logs.

The code looks like:

HttpTransporttransport=GoogleNetHttpTransport.newTrustedTransport();
JsonFactoryjsonFactory=GsonFactory.getDefaultInstance();
CloudResourceManager.BuilderresourceManagerBuilder=
newCloudResourceManager.Builder(
transport,jsonFactory,newHttpCredentialsAdapter(credentials))
.setApplicationName("Example Java App");
CloudResourceManagercloudResourceManager=resourceManagerBuilder.build();

Step 3: Create a Resource Object

A resource class represents a type of the resource managed by a service. The class is defined as an inner class of the service class. You can access them using the methods in the service class.

For example, you can get the "Projects" resource of the CloudResourceManager class:

importcom.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects;
...
Projectsprojects=cloudResourceManager.projects();

Step 4: Make a Request Object

The operations against the resource object are represented as request classes. The available operations depend on the resource class. For example, a resource class that provides "create", "get", and "delete" methods contain "Create", "Get", and "Delete" request classes respectively.

For Cloud Resource Manager's example, you can make the Get request object by calling projects.get method:

Getget=projects.get("projects/your-project-id");

Step 5: Execute the Request

A request object has the execute() method that runs the request. This call executes an HTTP request to the Google service and deserializes the JSON response to a model class. For example, the execute() method of the Get request object returns a Project object:

Projectproject=get.execute();
System.out.println("Project name: "+project.getDisplayName());

Summary

With these steps, you can make requests using Google APIs Client Library for Java. Here is the code snippet that combines all the steps using the Resource Manager service.

packagecom.example;
importcom.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
importcom.google.api.client.http.HttpTransport;
importcom.google.api.client.json.JsonFactory;
importcom.google.api.client.json.gson.GsonFactory;
importcom.google.api.services.cloudresourcemanager.v3.CloudResourceManager;
importcom.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects;
importcom.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects.Get;
importcom.google.api.services.cloudresourcemanager.v3.model.Project;
importcom.google.auth.http.HttpCredentialsAdapter;
importcom.google.auth.oauth2.GoogleCredentials;
publicclass ResourceManagerSample{
publicstaticvoidmain(String[]arguments)throwsException{
GoogleCredentialscredentials=GoogleCredentials.getApplicationDefault();
HttpTransporttransport=GoogleNetHttpTransport.newTrustedTransport();
JsonFactoryjsonFactory=GsonFactory.getDefaultInstance();
CloudResourceManager.BuilderresourceManagerBuilder=
newCloudResourceManager.Builder(
transport,jsonFactory,newHttpCredentialsAdapter(credentials))
.setApplicationName("Example Java App");
CloudResourceManagercloudResourceManager=resourceManagerBuilder.build();
Projectsprojects=cloudResourceManager.projects();
Getget=projects.get("projects/your-project-id");
Projectproject=get.execute();
System.out.println("Project display name: "+project.getDisplayName());
}
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024年08月12日 UTC.