Class ServiceAccountCredentials

    • Method Detail

      • 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 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.
        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 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.
        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 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.
        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
        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
      • 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.
        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.
        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:
        createScopedRequired in class GoogleCredentials
        Returns:
        Whether the credentials require scopes to be specified.
      • idTokenWithAudience

        public IdToken idTokenWithAudience​(String targetAudience,
                                           List<IdTokenProvider.Option> options)
                                    throws IOException
        Returns a Google ID Token from the metadata server on ComputeEngine.
        Specified by:
        idTokenWithAudience in interface IdTokenProvider
        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

        public ServiceAccountCredentials createWithCustomRetryStrategy​(boolean defaultRetriesEnabled)
        Clones the service account with the specified default retries.
        Overrides:
        createWithCustomRetryStrategy in class GoogleCredentials
        Parameters:
        defaultRetriesEnabled - a flag enabling or disabling default retries
        Returns:
        GoogleCredentials with the specified retry configuration.
      • createScoped

        public GoogleCredentials createScoped​(Collection<String> newScopes)
        Clones the service account with the specified scopes.

        Should be called before use for instances with empty scopes.

        Overrides:
        createScoped in class GoogleCredentials
        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.

        Should be called before use for instances with empty scopes.

        Overrides:
        createScoped in class GoogleCredentials
        Parameters:
        newScopes - Collection of scopes to request.
        newDefaultScopes - Collection of default scopes to request.
        Returns:
        GoogleCredentials with requested scopes.
      • createWithCustomLifetime

        public ServiceAccountCredentials createWithCustomLifetime​(int lifetime)
        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

        public ServiceAccountCredentials createWithUseJwtAccessWithScope​(boolean useJwtAccessWithScope)
        Clones the service account with a new useJwtAccessWithScope value.
        Parameters:
        useJwtAccessWithScope - whether self signed JWT with scopes should be used
        Returns:
        the cloned service account credentials with the given useJwtAccessWithScope
      • createDelegated

        public GoogleCredentials createDelegated​(String user)
        Description copied from class: GoogleCredentials
        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.
        Overrides:
        createDelegated in class GoogleCredentials
        Parameters:
        user - User to impersonate.
        Returns:
        GoogleCredentials with a delegated user.
      • getClientId

        public final String getClientId()
      • getClientEmail

        public final String getClientEmail()
      • getPrivateKey

        public final PrivateKey getPrivateKey()
      • getPrivateKeyId

        public final String getPrivateKeyId()
      • getServiceAccountUser

        public final String getServiceAccountUser()
      • getProjectId

        public final String getProjectId()
      • getTokenServerUri

        public final URI getTokenServerUri()
      • getUseJwtAccessWithScope

        public boolean getUseJwtAccessWithScope()
      • sign

        public byte[] sign​(byte[] toSign)
        Description copied from interface: ServiceAccountSigner
        Signs the provided bytes using the private key associated with the service account.
        Specified by:
        sign in interface ServiceAccountSigner
        Parameters:
        toSign - bytes to sign
        Returns:
        signed bytes
      • jwtWithClaims

        public JwtCredentials jwtWithClaims​(JwtClaims newClaims)
        Returns a new JwtCredentials instance with modified claims.
        Specified by:
        jwtWithClaims in interface JwtProvider
        Parameters:
        newClaims - new claims. Any unspecified claim fields will default to the the current values.
        Returns:
        new credentials
      • getRequestMetadata

        public void getRequestMetadata​(URI uri,
                                       Executor executor,
                                       RequestMetadataCallback callback)
        Description copied from class: Credentials
        Get the current request metadata without blocking.

        This should be called by the transport layer on each request, and the data should be populated in headers or other context. The implementation can either call the callback inline or asynchronously. Either way it should never block in this method. The executor is provided for tasks that may block.

        The default implementation will just call Credentials.getRequestMetadata(URI) then the callback from the given executor.

        The convention for handling binary data is for the key in the returned map to end with "-bin" and for the corresponding values to be base64 encoded.

        Overrides:
        getRequestMetadata in class OAuth2Credentials
        Parameters:
        uri - URI of the entry point for the request.
        executor - Executor to perform the request.
        callback - Callback to execute when the request is finished.
      • getRequestMetadata

        public Map<String,​List<String>> getRequestMetadata​(URI uri)
                                                          throws IOException
        Provide the request metadata by putting an access JWT directly in the metadata.
        Overrides:
        getRequestMetadata in class OAuth2Credentials
        Parameters:
        uri - URI of the entry point for the request.
        Returns:
        The request metadata used for populating headers or other context.
        Throws:
        IOException - if there was an error getting up-to-date access. The exception should implement Retryable and isRetryable() will return true if the operation may be retried.