CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-api-client--google-api-client

The Google APIs Client Library for Java is a flexible, efficient, and powerful Java client library for accessing any HTTP-based API on the web, not just Google APIs.

Pending
Overview
Eval results
Files

oauth2-auth.mddocs/

OAuth2 Authentication

The Google API Client provides a complete OAuth 2.0 authentication system supporting various credential types including service accounts, user credentials, and compute engine metadata server authentication.

Core Imports

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.googleapis.compute.ComputeCredential;

Service Account Authentication

GoogleCredential (Deprecated)

Note: This class is deprecated. Use com.google.auth.oauth2.ServiceAccountCredentials from the google-auth-library-oauth2-http library instead.

@Deprecated
public class GoogleCredential extends Credential {
    public static GoogleCredential fromStream(InputStream keyStream) throws IOException;
    public static GoogleCredential fromStream(InputStream keyStream, HttpTransport transport, JsonFactory jsonFactory) throws IOException;
    
    public GoogleCredential createScoped(Collection<String> scopes);
    public GoogleCredential createScoped(String... scopes);
    public GoogleCredential createDelegated(String user);
    
    public final String getServiceAccountId();
    public final PrivateKey getServiceAccountPrivateKey();
    public final String getServiceAccountUser();
    public final Collection<String> getServiceAccountScopes();
    
    public static class Builder extends Credential.Builder {
        public GoogleCredential build();
        public Builder setServiceAccountId(String serviceAccountId);
        public Builder setServiceAccountPrivateKey(PrivateKey serviceAccountPrivateKey);
        public Builder setServiceAccountScopes(Collection<String> serviceAccountScopes);
        public Builder setServiceAccountUser(String serviceAccountUser);
    }
}

Usage Example:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import java.io.FileInputStream;
import java.util.Arrays;

// Load from service account key file
GoogleCredential credential = GoogleCredential.fromStream(
    new FileInputStream("path/to/service-account-key.json"))
    .createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));

// Create with builder
GoogleCredential credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId("service-account-email@project.iam.gserviceaccount.com")
    .setServiceAccountPrivateKey(privateKey)
    .setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"))
    .build();

User Authorization Flow

GoogleAuthorizationCodeFlow

OAuth 2.0 authorization code flow implementation for Google APIs.

public class GoogleAuthorizationCodeFlow extends AuthorizationCodeFlow {
    public final Set<String> getScopes();
    public final String getAccessType();
    public final String getApprovalPrompt();
    
    public static class Builder extends AuthorizationCodeFlow.Builder {
        public GoogleAuthorizationCodeFlow build();
        public Builder setAccessType(String accessType);
        public Builder setApprovalPrompt(String approvalPrompt);
        public Builder setScopes(Collection<String> scopes);
    }
}

GoogleAuthorizationCodeRequestUrl

URL builder for Google OAuth 2.0 authorization requests.

public class GoogleAuthorizationCodeRequestUrl extends AuthorizationCodeRequestUrl {
    public GoogleAuthorizationCodeRequestUrl(String clientId, String redirectUri, Collection<String> scopes);
    public GoogleAuthorizationCodeRequestUrl(GoogleClientSecrets clientSecrets, String redirectUri, Collection<String> scopes);
    
    public final String getAccessType();
    public GoogleAuthorizationCodeRequestUrl setAccessType(String accessType);
    
    public final String getApprovalPrompt();
    public GoogleAuthorizationCodeRequestUrl setApprovalPrompt(String approvalPrompt);
    
    public final String getIncludeGrantedScopes();
    public GoogleAuthorizationCodeRequestUrl setIncludeGrantedScopes(Boolean includeGrantedScopes);
}

GoogleAuthorizationCodeTokenRequest

Token request for Google OAuth 2.0 authorization code flow.

public class GoogleAuthorizationCodeTokenRequest extends AuthorizationCodeTokenRequest {
    public GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String clientId, String clientSecret, String code, String redirectUri);
    public GoogleAuthorizationCodeTokenRequest(HttpTransport transport, JsonFactory jsonFactory, GoogleClientSecrets clientSecrets, String code, String redirectUri);
}

Usage Example:

import com.google.api.client.googleapis.auth.oauth2.*;
import com.google.api.client.auth.oauth2.Credential;
import java.util.Arrays;

// Load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, 
    new FileReader("client_secrets.json"));

// Create authorization flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, jsonFactory, clientSecrets,
    Arrays.asList("https://www.googleapis.com/auth/drive"))
    .setDataStoreFactory(dataStoreFactory)
    .setAccessType("offline")
    .build();

// Generate authorization URL
String redirectUri = "urn:ietf:wg:oauth:2.0:oob";
GoogleAuthorizationCodeRequestUrl authorizationUrl = flow.newAuthorizationUrl()
    .setRedirectUri(redirectUri);

// Exchange authorization code for tokens
GoogleAuthorizationCodeTokenRequest tokenRequest = 
    new GoogleAuthorizationCodeTokenRequest(
        httpTransport, jsonFactory, clientSecrets, 
        authorizationCode, redirectUri);
GoogleTokenResponse tokenResponse = tokenRequest.execute();

Client Secrets

GoogleClientSecrets

OAuth 2.0 client secrets for installed applications.

public final class GoogleClientSecrets extends GenericJson {
    public Details getInstalled();
    public GoogleClientSecrets setInstalled(Details installed);
    
    public Details getWeb();
    public GoogleClientSecrets setWeb(Details web);
    
    public static GoogleClientSecrets load(JsonFactory jsonFactory, Reader reader) throws IOException;
    public static GoogleClientSecrets load(JsonFactory jsonFactory, InputStream inputStream) throws IOException;
    
    public static final class Details extends GenericJson {
        public String getClientId();
        public Details setClientId(String clientId);
        
        public String getClientSecret();
        public Details setClientSecret(String clientSecret);
        
        public List<String> getRedirectUris();
        public Details setRedirectUris(List<String> redirectUris);
        
        public String getAuthUri();
        public Details setAuthUri(String authUri);
        
        public String getTokenUri();
        public Details setTokenUri(String tokenUri);
    }
}

Token Handling

GoogleTokenResponse

Google OAuth 2.0 token response.

public class GoogleTokenResponse extends TokenResponse {
    public final String getIdToken();
    public GoogleTokenResponse setIdToken(String idToken);
}

GoogleRefreshTokenRequest

Refresh token request for Google OAuth 2.0.

public class GoogleRefreshTokenRequest extends RefreshTokenRequest {
    public GoogleRefreshTokenRequest(HttpTransport transport, JsonFactory jsonFactory, String refreshToken, String clientId, String clientSecret);
}

ID Token Verification

GoogleIdToken

Google ID token representation and verification.

public class GoogleIdToken extends JsonWebSignature {
    public final Payload getPayload();
    
    public static GoogleIdToken parse(JsonFactory jsonFactory, String idTokenString) throws IOException;
    
    public static class Payload extends JsonWebToken.Payload {
        public String getEmail();
        public Payload setEmail(String email);
        
        public Boolean getEmailVerified();
        public Payload setEmailVerified(Boolean emailVerified);
        
        public String getHostedDomain();
        public Payload setHostedDomain(String hostedDomain);
        
        public String getName();
        public Payload setName(String name);
        
        public String getPicture();
        public Payload setPicture(String picture);
        
        public String getGivenName();
        public Payload setGivenName(String givenName);
        
        public String getFamilyName();
        public Payload setFamilyName(String familyName);
        
        public String getLocale();
        public Payload setLocale(String locale);
    }
}

GoogleIdTokenVerifier

Verifier for Google ID tokens.

public class GoogleIdTokenVerifier extends IdTokenVerifier {
    public GoogleIdToken verify(String idTokenString) throws GeneralSecurityException, IOException;
    
    public static class Builder extends IdTokenVerifier.Builder {
        public GoogleIdTokenVerifier build();
        public Builder setAudience(Collection<String> audience);
        public Builder setIssuer(String issuer); 
    }
}

Compute Engine Authentication

ComputeCredential

OAuth 2.0 credential for accessing Google APIs using the Compute Engine metadata server.

public class ComputeCredential extends Credential {
    public ComputeCredential();
    public ComputeCredential(Builder builder);
    
    public static class Builder extends Credential.Builder {
        public ComputeCredential build();
    }
}

Usage Example:

import com.google.api.client.googleapis.compute.ComputeCredential;

// Create credential for Compute Engine
ComputeCredential credential = new ComputeCredential.Builder(httpTransport, jsonFactory)
    .build();

Utilities

OAuth2Utils

Utility methods for OAuth 2.0 operations.

public class OAuth2Utils {
    public static final String BEARER_TOKEN_PREFIX = "Bearer ";
    
    public static void useProxy(HttpExecuteInterceptor proxy);
}

GoogleOAuthConstants

Constants for Google OAuth 2.0 endpoints.

public class GoogleOAuthConstants {
    public static final String AUTHORIZATION_SERVER_URL = "https://accounts.google.com/o/oauth2/v2/auth";
    public static final String TOKEN_SERVER_URL = "https://oauth2.googleapis.com/token";
    public static final String REVOKE_URL = "https://oauth2.googleapis.com/revoke";
    public static final String OOB_REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
}

Types

Credential

Base credential class (from google-oauth-client library).

HttpTransport

HTTP transport interface.

JsonFactory

JSON factory interface.

PrivateKey

Java security private key.

Collection<String>

Java collection of strings for scopes.

InputStream

Java input stream for reading credential files.

IOException

Exception for I/O operations.

GeneralSecurityException

Exception for security-related operations.

Install with Tessl CLI

npx tessl i tessl/maven-com-google-api-client--google-api-client

docs

batch-operations.md

client-services.md

http-transport.md

index.md

json-error-handling.md

media-operations.md

oauth2-auth.md

utilities.md

tile.json