Set Up Your Application

Before you can make requests to the API, you must set up authorization. If you are using a client library, you must also create a Service object.

The following code demonstrates how to configure your client and authorize requests using OAuth 2.0 for installed applications.

For a brief overview of authorization, read about how to authorize requests and identify your application to Google.

Java

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.cloudbilling.Cloudbilling;
import com.google.api.services.cloudbilling.model.ListBillingAccountsResponse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

...

  public void setUp() throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // Go to the Google Cloud Platform Console, open your application's
    // credentials page, and copy the client ID and client secret.
    // Then paste them into the following code.
    String clientId = "YOUR_CLIENT_ID";
    String clientSecret = "YOUR_CLIENT_SECRET";

    // Or your redirect URL for web based applications.
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
    List scopes = Arrays.asList(
        "https://www.googleapis.com/auth/cloud-billing",
        "https://www.googleapis.com/auth/cloud-billing.readonly",
        "https://www.googleapis.com/auth/cloud-platform");

    // Step 1: Authorize -->
    String authorizationUrl =
        new GoogleAuthorizationCodeRequestUrl(clientId, redirectUrl, scopes).build();

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);

    // Read the authorization code from the standard input stream.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = in.readLine();
    // End of Step 1 <--

    // Step 2: Exchange -->
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(httpTransport, jsonFactory, clientId, clientSecret, scopes);
    GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(redirectUrl).execute();
    Credential credential = flow.createAndStoreCredential(response, null);
    // End of Step 2 <--

    Cloudbilling service = new Cloudbilling.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("YOUR_APPLICATION_NAME").build();
    ...
  }
...

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.