API Requests

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 .

  GoogleCredentials 
  
 credentials 
  
 = 
  
 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:

  HttpTransport 
  
 transport 
  
 = 
  
 GoogleNetHttpTransport 
 . 
 newTrustedTransport 
 (); 
 JsonFactory 
  
 jsonFactory 
  
 = 
  
 GsonFactory 
 . 
 getDefaultInstance 
 (); 
 CloudResourceManager 
 . 
 Builder 
  
 resourceManagerBuilder 
  
 = 
  
 new 
  
 CloudResourceManager 
 . 
 Builder 
 ( 
  
 transport 
 , 
  
 jsonFactory 
 , 
  
 new 
  
 HttpCredentialsAdapter 
 ( 
 credentials 
 )) 
  
 . 
 setApplicationName 
 ( 
 "Example Java App" 
 ); 
 CloudResourceManager 
  
 cloudResourceManager 
  
 = 
  
 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:

  import 
  
 com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects 
 ; 
 ... 
 Projects 
  
 projects 
  
 = 
  
 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:

  Get 
  
 get 
  
 = 
  
 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:

  Project 
  
 project 
  
 = 
  
 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.

  package 
  
 com.example 
 ; 
 import 
  
 com.google.api.client.googleapis.javanet.GoogleNetHttpTransport 
 ; 
 import 
  
 com.google.api.client.http.HttpTransport 
 ; 
 import 
  
 com.google.api.client.json.JsonFactory 
 ; 
 import 
  
 com.google.api.client.json.gson.GsonFactory 
 ; 
 import 
  
 com.google.api.services.cloudresourcemanager.v3.CloudResourceManager 
 ; 
 import 
  
 com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects 
 ; 
 import 
  
 com.google.api.services.cloudresourcemanager.v3.CloudResourceManager.Projects.Get 
 ; 
 import 
  
 com.google.api.services.cloudresourcemanager.v3.model.Project 
 ; 
 import 
  
 com.google.auth.http.HttpCredentialsAdapter 
 ; 
 import 
  
 com.google.auth.oauth2.GoogleCredentials 
 ; 
 public 
  
 class 
 ResourceManagerSample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 arguments 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 GoogleCredentials 
  
 credentials 
  
 = 
  
 GoogleCredentials 
 . 
 getApplicationDefault 
 (); 
  
 HttpTransport 
  
 transport 
  
 = 
  
 GoogleNetHttpTransport 
 . 
 newTrustedTransport 
 (); 
  
 JsonFactory 
  
 jsonFactory 
  
 = 
  
 GsonFactory 
 . 
 getDefaultInstance 
 (); 
  
 CloudResourceManager 
 . 
 Builder 
  
 resourceManagerBuilder 
  
 = 
  
 new 
  
 CloudResourceManager 
 . 
 Builder 
 ( 
  
 transport 
 , 
  
 jsonFactory 
 , 
  
 new 
  
 HttpCredentialsAdapter 
 ( 
 credentials 
 )) 
  
 . 
 setApplicationName 
 ( 
 "Example Java App" 
 ); 
  
 CloudResourceManager 
  
 cloudResourceManager 
  
 = 
  
 resourceManagerBuilder 
 . 
 build 
 (); 
  
 Projects 
  
 projects 
  
 = 
  
 cloudResourceManager 
 . 
 projects 
 (); 
  
 Get 
  
 get 
  
 = 
  
 projects 
 . 
 get 
 ( 
 "projects/your-project-id" 
 ); 
  
 Project 
  
 project 
  
 = 
  
 get 
 . 
 execute 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Project display name: " 
  
 + 
  
 project 
 . 
 getDisplayName 
 ()); 
  
 } 
 } 
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: