A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/api-client-library/java/google-api-java-client/requests below:

API Requests | API Client Library for Java

API Requests

Stay organized with collections Save and categorize content based on your preferences.

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());
  }
}

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.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-08-12 UTC."],[[["This guide provides a step-by-step approach to making requests to Google APIs using the Google APIs Client Library for Java, covering authentication, service instantiation, resource object creation, request object creation, and request execution."],["Developers will learn how to authenticate using GoogleCredentials, instantiate service classes to interact with specific Google APIs, and create resource objects to access specific API resources."],["You'll be able to utilize request objects to perform operations on resources, with a working example demonstrating the process using the Cloud Resource Manager service to retrieve project details."],["The provided code examples allow developers to easily adapt the steps for their own applications and make API requests efficiently."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4