Class AuthGoogle
- All Implemented Interfaces:
AutoCloseable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClaims(JsonObject json)Adds additional custom claims to the JWT claims set.The access token to be used in Google API requests.The client email address of the service account.intThe expiration time, in seconds, of the access token to be requested.intgetIat()This property can be set to override the default current date/time value for theiatclaim of the JWT.The JSON key for obtaining an access token.intIf the access token is valid, contains the number of seconds remaining until it expires.getP12()Returns the private key in a PFX (P12) object.Returns the installed listener, ornull.getScope()A space-delimited list of the permissions that the application requests.The email address of the user for which the application is requesting delegated access.booleangetValid()trueif the AccessToken property contains a valid non-expired access token obtained via the call to ObtainAccessToken.voidobtainAccessToken(Socket connection)Sends the HTTP request to fetch the access token.voidonAbortCheck(BooleanSupplier callback)Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it.voidonPercentDone(IntPredicate callback)Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it.voidonProgressInfo(BiConsumer<String,String> callback)Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds.voidsetAccessToken(String value)Sets theAccessTokenproperty; seegetAccessToken().voidsetEmailAddress(String value)Sets theEmailAddressproperty; seegetEmailAddress().voidsetExpireNumSeconds(int value)Sets theExpireNumSecondsproperty; seegetExpireNumSeconds().voidsetIat(int value)Sets theIatproperty; seegetIat().voidsetJsonKey(String value)Sets theJsonKeyproperty; seegetJsonKey().voidSets the P12 private key to be used for obtaining an access token.voidsetProgressListener(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;nullremoves it.voidSets theScopeproperty; seegetScope().voidsetSubEmailAddress(String value)Sets theSubEmailAddressproperty; seegetSubEmailAddress().Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
AuthGoogle
public AuthGoogle()Creates a newAuthGoogleobject.
-
-
Method Details
-
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
AccessTokenproperty
-
setAccessToken
Sets theAccessTokenproperty; seegetAccessToken().- Parameters:
value- the new value (notnull)
-
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
EmailAddressproperty
-
setEmailAddress
Sets theEmailAddressproperty; seegetEmailAddress().- Parameters:
value- the new value (notnull)
-
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
ExpireNumSecondsproperty
-
setExpireNumSeconds
public void setExpireNumSeconds(int value)Sets theExpireNumSecondsproperty; seegetExpireNumSeconds().- Parameters:
value- the new value
-
getIat
public int getIat()This property can be set to override the default current date/time value for theiatclaim 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
Iatproperty
-
setIat
public void setIat(int value)Sets theIatproperty; seegetIat().- Parameters:
value- the new value
-
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
JsonKeyproperty
-
setJsonKey
Sets theJsonKeyproperty; seegetJsonKey().- Parameters:
value- the new value (notnull)
-
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
NumSecondsRemainingproperty (read-only)
-
getScope
A space-delimited list of the permissions that the application requests.- Returns:
- the value of the
Scopeproperty
-
setScope
Sets theScopeproperty; seegetScope().- Parameters:
value- the new value (notnull)
-
getSubEmailAddress
The email address of the user for which the application is requesting delegated access.- Returns:
- the value of the
SubEmailAddressproperty
-
setSubEmailAddress
Sets theSubEmailAddressproperty; seegetSubEmailAddress().- Parameters:
value- the new value (notnull)
-
getValid
public boolean getValid()trueif the AccessToken property contains a valid non-expired access token obtained via the call to ObtainAccessToken.- Returns:
- the value of the
Validproperty (read-only)
-
addClaims
Adds additional custom claims to the JWT claims set.- Parameters:
json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
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
Pfxowned by the caller (nevernull); close it when done - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
obtainAccessToken
Sends the HTTP request to fetch the access token. When this method completes successfully, the access token is available in the AccessToken property. Theconnectionis 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- theconnectionargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setP12
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- thekeyargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setProgressListener
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;nullremoves it.- Parameters:
listener- the listener, ornull- Throws:
IllegalStateException- if called from inside one of this object's own callbacks
-
getProgressListener
Returns the installed listener, ornull.- Returns:
- the listener installed with
setProgressListener(ProgressListener)or one of theon...methods
-
onAbortCheck
Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onPercentDone
Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onProgressInfo
Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-