Class AuthGoogle

java.lang.Object
com.chilkatsoft.chilkat2.ChilkatObject
com.chilkatsoft.chilkat2.AuthGoogle
All Implemented Interfaces:
AutoCloseable

public class AuthGoogle extends ChilkatObject
<section class="authgoogle-summary">

Obtain Google OAuth 2.0 access tokens using a service account.

Chilkat.AuthGoogle obtains OAuth 2.0 access tokens for Google Cloud Platform APIs and Google Apps APIs using a Google service account. It supports JSON private keys and P12 private keys, OAuth scopes, optional delegated user access, custom JWT claims, access-token validity tracking, token lifetime settings, and integration with Chilkat request classes that need a Google bearer token.

Service account auth

Authenticate server-to-server using a Google service account rather than an interactive browser-based user consent flow.

JSON or P12 keys

Load service account credentials from modern JSON private keys or from legacy P12 private key files.

Scopes and claims

Specify the OAuth scopes required by the target Google API and add custom JWT claims when needed.

Delegated user access

Use domain-wide delegation by setting a subject email when the service account is authorized to act on behalf of a Google Workspace user.

Access-token tracking

Inspect the generated access token, expiration time, remaining lifetime, and validity state before using or refreshing credentials.

Chilkat API integration

Pass the resulting bearer token to Chilkat HTTP, REST, storage, mail, or other Google API request workflows.

Common pattern: Load the service account JSON key or P12 key, set the service account email, required Scope, optional delegated SubEmailAddress, and token lifetime, then call ObtainAccessToken. Use AuthGoogle for service-account OAuth flows; use Chilkat.OAuth2 when the application needs an interactive three-legged OAuth flow involving a user's browser consent.

For an extended overview, see AuthGoogle Class Overview.

</section>

Event-capable: long-running methods report progress through a ProgressListener installed with setProgressListener(ProgressListener), onAbortCheck(BooleanSupplier), onPercentDone(IntPredicate) or onProgressInfo(BiConsumer).

Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when done; an unclosed instance is freed when it becomes unreachable.

See Also:
Online reference
  • Constructor Details

    • AuthGoogle

      public AuthGoogle()
      Creates a new AuthGoogle object.
  • Method Details

    • getAccessToken

      public String getAccessToken()
      The access token to be used in Google API requests. This property is set on a successful call to ObtainAccessToken.

      Important: This class is used for authenticating calls to the Google Cloud Platform API and Google Apps API using a service account.. For 3-legged OAuth2, where a browser must be used to interactively get permission from the Google account owner, use the Chilkat OAuth2 class/object.

      Returns:
      the value of the AccessToken property
    • setAccessToken

      public void setAccessToken​(String value)
      Sets the AccessToken property; see getAccessToken().
      Parameters:
      value - the new value (not null)
    • getEmailAddress

      public String getEmailAddress()
      The client email address of the service account. If a JSON key is used, then the client_email should already be specified within the JSON key, and this property is unused. This property must be set if using a P12 key.
      Returns:
      the value of the EmailAddress property
    • setEmailAddress

      public void setEmailAddress​(String value)
      Sets the EmailAddress property; see getEmailAddress().
      Parameters:
      value - the new value (not null)
    • getExpireNumSeconds

      public int getExpireNumSeconds()
      The expiration time, in seconds, of the access token to be requested. The maximum value is 1 hour (3600 seconds). The default value is 3600.
      Returns:
      the value of the ExpireNumSeconds property
    • setExpireNumSeconds

      public void setExpireNumSeconds​(int value)
      Sets the ExpireNumSeconds property; see getExpireNumSeconds().
      Parameters:
      value - the new value
    • getIat

      public int getIat()
      This property can be set to override the default current date/time value for the iat claim of the JWT. It can be set to a value indicating the number of seconds from 1970-01-01T00:00:00Z UTC.

      The default value is 0, which indicates to use the iat value for the current system date/time. Unless explicitly needed, always leave this property at the default value.

      Returns:
      the value of the Iat property
    • setIat

      public void setIat​(int value)
      Sets the Iat property; see getIat().
      Parameters:
      value - the new value
    • getJsonKey

      public String getJsonKey()
      The JSON key for obtaining an access token. An application must set either the P12 or JSON private key, but not both.
      Returns:
      the value of the JsonKey property
    • setJsonKey

      public void setJsonKey​(String value)
      Sets the JsonKey property; see getJsonKey().
      Parameters:
      value - the new value (not null)
    • getNumSecondsRemaining

      public int getNumSecondsRemaining()
      If the access token is valid, contains the number of seconds remaining until it expires. A value of 0 indicates an invalid or expired access token.
      Returns:
      the value of the NumSecondsRemaining property (read-only)
    • getScope

      public String getScope()
      A space-delimited list of the permissions that the application requests.
      Returns:
      the value of the Scope property
    • setScope

      public void setScope​(String value)
      Sets the Scope property; see getScope().
      Parameters:
      value - the new value (not null)
    • getSubEmailAddress

      public String getSubEmailAddress()
      The email address of the user for which the application is requesting delegated access.
      Returns:
      the value of the SubEmailAddress property
    • setSubEmailAddress

      public void setSubEmailAddress​(String value)
      Sets the SubEmailAddress property; see getSubEmailAddress().
      Parameters:
      value - the new value (not null)
    • getValid

      public boolean getValid()
      true if the AccessToken property contains a valid non-expired access token obtained via the call to ObtainAccessToken.
      Returns:
      the value of the Valid property (read-only)
    • addClaims

      public void addClaims​(JsonObject json)
      Adds additional custom claims to the JWT claims set.
      Parameters:
      json - the json argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • getP12

      public Pfx getP12()
      Returns the private key in a PFX (P12) object. This is only possible if the private key was previously set by calling SetP12.
      Returns:
      a new Pfx owned by the caller (never null); close it when done
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • obtainAccessToken

      public void obtainAccessToken​(Socket connection)
      Sends the HTTP request to fetch the access token. When this method completes successfully, the access token is available in the AccessToken property. The connection is an existing connection to www.googleapis.com.

      Important: Make sure your computer's date/time is accurately set to the current date/time, otherwise you'll get a 400 response status code with this error: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values and use a clock with skew to account for clock differences between systems..

      Parameters:
      connection - the connection argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • setP12

      public void setP12​(Pfx key)
      Sets the P12 private key to be used for obtaining an access token. An application must set either the P12 or JSON private key, but not both.
      Parameters:
      key - the key argument (not null)
      Throws:
      ChilkatException - if the operation fails; ChilkatException.getLastErrorText() says why
    • setProgressListener

      public void setProgressListener​(ProgressListener listener)
      Installs the listener that receives this object's progress events (abortCheck, percentDone, progressInfo) during its long-running methods; replaces any listener or lambda installed before; null removes it.
      Parameters:
      listener - the listener, or null
      Throws:
      IllegalStateException - if called from inside one of this object's own callbacks
    • getProgressListener

      public ProgressListener getProgressListener()
      Returns the installed listener, or null.
      Returns:
      the listener installed with setProgressListener(ProgressListener) or one of the on... methods
    • onAbortCheck

      public void onAbortCheck​(BooleanSupplier callback)
      Installs a listener whose only callback is abortCheck: called every HeartbeatMs milliseconds during a long-running method; return true to abort it. Replaces any listener installed before.
      Parameters:
      callback - the callback, or null to remove the listener
    • onPercentDone

      public void onPercentDone​(IntPredicate callback)
      Installs a listener whose only callback is percentDone: called with the completion percentage (0..100) as a long-running method progresses; return true to abort it. Replaces any listener installed before.
      Parameters:
      callback - the callback, or null to remove the listener
    • onProgressInfo

      public void onProgressInfo​(BiConsumer<String,​String> callback)
      Installs a listener whose only callback is progressInfo: called with a name/value pair of progress information as a long-running method proceeds. Replaces any listener installed before.
      Parameters:
      callback - the callback, or null to remove the listener