Class ServiceAccountCredentials
- All Implemented Interfaces:
IdTokenProvider,JwtProvider,QuotaProjectIdProvider,ServiceAccountSigner,Serializable
By default uses a JSON Web Token (JWT) to fetch access tokens.
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.google.auth.oauth2.OAuth2Credentials
OAuth2Credentials.CredentialsChangedListenerNested classes/interfaces inherited from interface com.google.auth.oauth2.IdTokenProvider
IdTokenProvider.OptionNested classes/interfaces inherited from interface com.google.auth.ServiceAccountSigner
ServiceAccountSigner.SigningException -
Field Summary
Fields inherited from class com.google.auth.oauth2.GoogleCredentials
quotaProjectIdFields inherited from class com.google.auth.Credentials
GOOGLE_DEFAULT_UNIVERSE -
Method Summary
Modifier and TypeMethodDescriptioncreateDelegated(String user) If the credentials support domain-wide delegation, creates a copy of the identity so that it impersonates the specified user; otherwise, returns the same instance.createScoped(Collection<String> newScopes) Clones the service account with the specified scopes.createScoped(Collection<String> newScopes, Collection<String> newDefaultScopes) Clones the service account with the specified scopes.booleanReturns whether the scopes are empty, meaning createScoped must be called before use.createWithCustomLifetime(int lifetime) Clones the service account with a new lifetime value.createWithCustomRetryStrategy(boolean defaultRetriesEnabled) Clones the service account with the specified default retries.createWithUseJwtAccessWithScope(boolean useJwtAccessWithScope) Clones the service account with a new useJwtAccessWithScope value.booleanstatic ServiceAccountCredentialsfromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes) Factory with minimum identifying information using PKCS#8 for the private key.static ServiceAccountCredentialsfromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, HttpTransportFactory transportFactory, URI tokenServerUri) Factory with minimum identifying information and custom transport using PKCS#8 for the private key.static ServiceAccountCredentialsfromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) Factory with minimum identifying information and custom transport using PKCS#8 for the private key.static ServiceAccountCredentialsfromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes) Factory with minimum identifying information using PKCS#8 for the private key.static ServiceAccountCredentialsfromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes, HttpTransportFactory transportFactory, URI tokenServerUri) Factory with minimum identifying information and custom transport using PKCS#8 for the private key.static ServiceAccountCredentialsfromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) Factory with minimum identifying information and custom transport using PKCS#8 for the private key.static ServiceAccountCredentialsfromStream(InputStream credentialsStream) Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.static ServiceAccountCredentialsfromStream(InputStream credentialsStream, HttpTransportFactory transportFactory) Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.final Stringfinal Stringfinal Collection<String>final PrivateKeyfinal Stringfinal StringThe projectId value for a Credential type.getRequestMetadata(URI uri) Provide the request metadata by putting an access JWT directly in the metadata.voidgetRequestMetadata(URI uri, Executor executor, RequestMetadataCallback callback) final Collection<String>final Stringfinal URIbooleaninthashCode()idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) Returns a Google ID Token from either the OAuth or IAM Endpoint.jwtWithClaims(JwtClaims newClaims) Returns a new JwtCredentials instance with modified claims.Refreshes the OAuth2 access token by getting a new access token using a JSON Web Token (JWT).byte[]sign(byte[] toSign) protected com.google.common.base.MoreObjects.ToStringHelperA helper for overriding the toString() method.Methods inherited from class com.google.auth.oauth2.GoogleCredentials
create, create, createScoped, createWithQuotaProject, getAdditionalHeaders, getApplicationDefault, getApplicationDefault, getCredentialInfo, getQuotaProjectId, getUniverseDomain, isExplicitUniverseDomain, toStringMethods inherited from class com.google.auth.oauth2.OAuth2Credentials
addChangeListener, getAccessToken, getAuthenticationType, getFromServiceLoader, getRequestMetadataInternal, hasRequestMetadata, hasRequestMetadataOnly, newInstance, refresh, refreshIfExpired, removeChangeListenerMethods inherited from class com.google.auth.Credentials
blockingGetToCallback, getRequestMetadata
-
Method Details
-
fromPkcs8
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes) throws IOException Factory with minimum identifying information using PKCS#8 for the private key.- Parameters:
clientId- Client ID of the service account from the console. May be null.clientEmail- Client email address of the service account from the console.privateKeyPkcs8- RSA private key object for the service account in PKCS#8 format.privateKeyId- Private key identifier for the service account. May be null.scopes- Scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.- Returns:
- New ServiceAccountCredentials created from a private key.
- Throws:
IOException- if the credential cannot be created from the private key.
-
fromPkcs8
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes) throws IOException Factory with minimum identifying information using PKCS#8 for the private key.- Parameters:
clientId- client ID of the service account from the console. May be null.clientEmail- client email address of the service account from the consoleprivateKeyPkcs8- RSA private key object for the service account in PKCS#8 format.privateKeyId- private key identifier for the service account. May be null.scopes- scope strings for the APIs to be called. May be null or an empty collection.defaultScopes- default scope strings for the APIs to be called. May be null or an empty.- Returns:
- new ServiceAccountCredentials created from a private key
- Throws:
IOException- if the credential cannot be created from the private key
-
fromPkcs8
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, HttpTransportFactory transportFactory, URI tokenServerUri) throws IOException Factory with minimum identifying information and custom transport using PKCS#8 for the private key.- Parameters:
clientId- Client ID of the service account from the console. May be null.clientEmail- Client email address of the service account from the console.privateKeyPkcs8- RSA private key object for the service account in PKCS#8 format.privateKeyId- Private key identifier for the service account. May be null.scopes- Scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.transportFactory- HTTP transport factory, creates the transport used to get access tokens.tokenServerUri- URI of the end point that provides tokens.- Returns:
- New ServiceAccountCredentials created from a private key.
- Throws:
IOException- if the credential cannot be created from the private key.
-
fromPkcs8
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes, HttpTransportFactory transportFactory, URI tokenServerUri) throws IOException Factory with minimum identifying information and custom transport using PKCS#8 for the private key.- Parameters:
clientId- client ID of the service account from the console. May be null.clientEmail- client email address of the service account from the consoleprivateKeyPkcs8- RSA private key object for the service account in PKCS#8 format.privateKeyId- private key identifier for the service account. May be null.scopes- scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.defaultScopes- default scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.transportFactory- HTTP transport factory, creates the transport used to get access tokens.tokenServerUri- URI of the end point that provides tokens- Returns:
- new ServiceAccountCredentials created from a private key
- Throws:
IOException- if the credential cannot be created from the private key
-
fromPkcs8
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) throws IOException Factory with minimum identifying information and custom transport using PKCS#8 for the private key.- Parameters:
clientId- Client ID of the service account from the console. May be null.clientEmail- Client email address of the service account from the console.privateKeyPkcs8- RSA private key object for the service account in PKCS#8 format.privateKeyId- Private key identifier for the service account. May be null.scopes- Scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.transportFactory- HTTP transport factory, creates the transport used to get access tokens.tokenServerUri- URI of the end point that provides tokens.serviceAccountUser- The email of the user account to impersonate, if delegating domain-wide authority to the service account.- Returns:
- New ServiceAccountCredentials created from a private key.
- Throws:
IOException- if the credential cannot be created from the private key.
-
fromPkcs8
public static ServiceAccountCredentials fromPkcs8(String clientId, String clientEmail, String privateKeyPkcs8, String privateKeyId, Collection<String> scopes, Collection<String> defaultScopes, HttpTransportFactory transportFactory, URI tokenServerUri, String serviceAccountUser) throws IOException Factory with minimum identifying information and custom transport using PKCS#8 for the private key.- Parameters:
clientId- client ID of the service account from the console. May be null.clientEmail- client email address of the service account from the consoleprivateKeyPkcs8- RSA private key object for the service account in PKCS#8 format.privateKeyId- private key identifier for the service account. May be null.scopes- scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.defaultScopes- default scope strings for the APIs to be called. May be null or an empty collection, which results in a credential that must have createScoped called before use.transportFactory- HTTP transport factory, creates the transport used to get access tokens.tokenServerUri- URI of the end point that provides tokensserviceAccountUser- the email of the user account to impersonate, if delegating domain-wide authority to the service account.- Returns:
- new ServiceAccountCredentials created from a private key
- Throws:
IOException- if the credential cannot be created from the private key
-
fromStream
public static ServiceAccountCredentials fromStream(InputStream credentialsStream) throws IOException Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.Important: If you accept a credential configuration (credential JSON/File/Stream) from an external source for authentication to Google Cloud Platform, you must validate it before providing it to any Google API or library. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. For more information, refer to .
- Parameters:
credentialsStream- the stream with the credential definition.- Returns:
- the credential defined by the credentialsStream.
- Throws:
IOException- if the credential cannot be created from the stream.
-
fromStream
public static ServiceAccountCredentials fromStream(InputStream credentialsStream, HttpTransportFactory transportFactory) throws IOException Returns credentials defined by a Service Account key file in JSON format from the Google Developers Console.Important: If you accept a credential configuration (credential JSON/File/Stream) from an external source for authentication to Google Cloud Platform, you must validate it before providing it to any Google API or library. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. For more information, refer to .
- Parameters:
credentialsStream- the stream with the credential definition.transportFactory- HTTP transport factory, creates the transport used to get access tokens.- Returns:
- the credential defined by the credentialsStream.
- Throws:
IOException- if the credential cannot be created from the stream.
-
createScopedRequired
public boolean createScopedRequired()Returns whether the scopes are empty, meaning createScoped must be called before use.- Overrides:
createScopedRequiredin classGoogleCredentials- Returns:
- Whether the credentials require scopes to be specified.
-
refreshAccessToken
Refreshes the OAuth2 access token by getting a new access token using a JSON Web Token (JWT).- Overrides:
refreshAccessTokenin classOAuth2Credentials- Returns:
- never
- Throws:
IOException
-
idTokenWithAudience
public IdToken idTokenWithAudience(String targetAudience, List<IdTokenProvider.Option> options) throws IOException Returns a Google ID Token from either the OAuth or IAM Endpoint. For Credentials that are in the Google Default Universe (googleapis.com), the ID Token will be retrieved from the Oauth Endpoint. Otherwise, it will be retrieved from the IAM Endpoint.- Specified by:
idTokenWithAudiencein interfaceIdTokenProvider- Parameters:
targetAudience- the aud: field the IdToken should include.options- list of Credential specific options for the token. Currently, unused for ServiceAccountCredentials.- Returns:
- IdToken object which includes the raw id_token, expiration and audience
- Throws:
IOException- if the attempt to get an IdToken failed
-
createWithCustomRetryStrategy
Clones the service account with the specified default retries.- Overrides:
createWithCustomRetryStrategyin classGoogleCredentials- Parameters:
defaultRetriesEnabled- a flag enabling or disabling default retries- Returns:
- GoogleCredentials with the specified retry configuration.
-
createScoped
Clones the service account with the specified scopes.Should be called before use for instances with empty scopes.
- Overrides:
createScopedin classGoogleCredentials- Parameters:
newScopes- Collection of scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
createScoped
public GoogleCredentials createScoped(Collection<String> newScopes, Collection<String> newDefaultScopes) Clones the service account with the specified scopes. The Access Token is invalidated even if the same scopes are provided. Access Tokens contain information of the internal values (i.e. scope). If an internal value (scope) is modified, then the existing Access Token is no longer valid and should not be re-used.Should be called before use for instances with empty scopes.
- Overrides:
createScopedin classGoogleCredentials- Parameters:
newScopes- Collection of scopes to request.newDefaultScopes- Collection of default scopes to request.- Returns:
- GoogleCredentials with requested scopes.
-
createWithCustomLifetime
Clones the service account with a new lifetime value.- Parameters:
lifetime- life time value in seconds. The value should be at most 43200 (12 hours). If the token is used for calling a Google API, then the value should be at most 3600 (1 hour). If the given value is 0, then the default value 3600 will be used when creating the credentials.- Returns:
- the cloned service account credentials with the given custom life time
-
createWithUseJwtAccessWithScope
Clones the service account with a new useJwtAccessWithScope value. This flag will be ignored if universeDomain field is different fromCredentials.GOOGLE_DEFAULT_UNIVERSE.- Parameters:
useJwtAccessWithScope- whether self-signed JWT with scopes should be used- Returns:
- the cloned service account credentials with the given useJwtAccessWithScope
-
createDelegated
Description copied from class:GoogleCredentialsIf the credentials support domain-wide delegation, creates a copy of the identity so that it impersonates the specified user; otherwise, returns the same instance.- Overrides:
createDelegatedin classGoogleCredentials- Parameters:
user- User to impersonate.- Returns:
- GoogleCredentials with a delegated user.
-
getClientId
-
getClientEmail
-
getPrivateKey
-
getPrivateKeyId
-
getScopes
-
getDefaultScopes
-
getServiceAccountUser
-
getProjectId
Description copied from class:GoogleCredentialsThe projectId value for a Credential type. Since not all GoogleCredentials subclass have a projectId associated, the projectId may be null. A subset of GoogleCredentials subclasses will override to return their projectId.- Overrides:
getProjectIdin classGoogleCredentials- Returns:
- the projectId set in the SA Key file or the user set projectId
-
getTokenServerUri
-
getUseJwtAccessWithScope
public boolean getUseJwtAccessWithScope() -
getAccount
- Specified by:
getAccountin interfaceServiceAccountSigner
-
sign
public byte[] sign(byte[] toSign) - Specified by:
signin interfaceServiceAccountSigner
-
jwtWithClaims
Returns a new JwtCredentials instance with modified claims.- Specified by:
jwtWithClaimsin interfaceJwtProvider- Parameters:
newClaims- new claims. Any unspecified claim fields will default to the current values.- Returns:
- new credentials
-
hashCode
public int hashCode()- Overrides:
hashCodein classGoogleCredentials
-
toStringHelper
protected com.google.common.base.MoreObjects.ToStringHelper toStringHelper()Description copied from class:GoogleCredentialsA helper for overriding the toString() method. This allows inheritance of super class fields. Extending classes can override this implementation and call super implementation and add more fields. Same cannot be done with overriding the toString() directly.- Overrides:
toStringHelperin classGoogleCredentials- Returns:
- an instance of the ToStringHelper that has public fields added
-
equals
- Overrides:
equalsin classGoogleCredentials
-
getRequestMetadata
- Overrides:
getRequestMetadatain classOAuth2Credentials
-
getRequestMetadata
Provide the request metadata by putting an access JWT directly in the metadata.- Overrides:
getRequestMetadatain classOAuth2Credentials- Throws:
IOException
-
getMetricsCredentialType
- Overrides:
getMetricsCredentialTypein classCredentials
-
newBuilder
-
toBuilder
- Overrides:
toBuilderin classGoogleCredentials
-