Class GoogleCredentials

    • Field Detail

      • quotaProjectId

        protected final String quotaProjectId
    • Constructor Detail

      • GoogleCredentials

        protected GoogleCredentials()
        Default constructor.
      • GoogleCredentials

        protected GoogleCredentials​(AccessToken accessToken,
                                    String quotaProjectId)
      • GoogleCredentials

        public GoogleCredentials​(AccessToken accessToken)
        Constructor with explicit access token.
        Parameters:
        accessToken - initial or temporary access token
      • GoogleCredentials

        protected GoogleCredentials​(AccessToken accessToken,
                                    Duration refreshMargin,
                                    Duration expirationMargin)
        Constructor with explicit access token and refresh times
        Parameters:
        accessToken - initial or temporary access token
    • Method Detail

      • create

        public static GoogleCredentials create​(AccessToken accessToken)
        Returns the credentials instance from the given access token.
        Parameters:
        accessToken - the access token
        Returns:
        the credentials instance
      • getApplicationDefault

        public static GoogleCredentials getApplicationDefault()
                                                       throws IOException
        Returns the Application Default Credentials.

        Returns the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:

        1. Credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
        2. Credentials provided by the Google Cloud SDK.
          1. gcloud auth application-default login for user account credentials.
          2. gcloud auth application-default login --impersonate-service-account for impersonated service account credentials.
        3. Google App Engine built-in credentials
        4. Google Cloud Shell built-in credentials
        5. Google Compute Engine built-in credentials
        Returns:
        the credentials instance.
        Throws:
        IOException - if the credentials cannot be created in the current environment.
      • getApplicationDefault

        public static GoogleCredentials getApplicationDefault​(HttpTransportFactory transportFactory)
                                                       throws IOException
        Returns the Application Default Credentials.

        Returns the Application Default Credentials which are used to identify and authorize the whole application. The following are searched (in order) to find the Application Default Credentials:

        1. Credentials file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
        2. Credentials provided by the Google Cloud SDK gcloud auth application-default login command
        3. Google App Engine built-in credentials
        4. Google Cloud Shell built-in credentials
        5. Google Compute Engine built-in credentials
        Parameters:
        transportFactory - HTTP transport factory, creates the transport used to get access tokens.
        Returns:
        the credentials instance.
        Throws:
        IOException - if the credentials cannot be created in the current environment.
      • fromStream

        public static GoogleCredentials fromStream​(InputStream credentialsStream)
                                            throws IOException
        Returns credentials defined by a JSON file stream.

        The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.

        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 GoogleCredentials fromStream​(InputStream credentialsStream,
                                                   HttpTransportFactory transportFactory)
                                            throws IOException
        Returns credentials defined by a JSON file stream.

        The stream can contain a Service Account key file in JSON format from the Google Developers Console or a stored user credential using the format supported by the Cloud SDK.

        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.
      • createWithQuotaProject

        public GoogleCredentials createWithQuotaProject​(String quotaProject)
        Creates a credential with the provided quota project.
        Parameters:
        quotaProject - the quota project to set on the credential
        Returns:
        credential with the provided quota project
      • createScopedRequired

        public boolean createScopedRequired()
        Indicates whether the credentials require scopes to be specified via a call to createScoped(java.util.Collection<java.lang.String>) before use.
        Returns:
        Whether the credentials require scopes to be specified.
      • createScoped

        public GoogleCredentials createScoped​(Collection<String> scopes)
        If the credentials support scopes, creates a copy of the identity with the specified scopes; otherwise, returns the same instance.
        Parameters:
        scopes - Collection of scopes to request.
        Returns:
        GoogleCredentials with requested scopes.
      • createScoped

        public GoogleCredentials createScoped​(Collection<String> scopes,
                                              Collection<String> defaultScopes)
        If the credentials support scopes, creates a copy of the identity with the specified scopes and default scopes; otherwise, returns the same instance. This is mainly used by client libraries.
        Parameters:
        scopes - Collection of scopes to request.
        defaultScopes - Collection of default scopes to request.
        Returns:
        GoogleCredentials with requested scopes.
      • createScoped

        public GoogleCredentials createScoped​(String... scopes)
        If the credentials support scopes, creates a copy of the identity with the specified scopes; otherwise, returns the same instance.
        Parameters:
        scopes - Collection of scopes to request.
        Returns:
        GoogleCredentials with requested scopes.
      • createWithCustomRetryStrategy

        public GoogleCredentials createWithCustomRetryStrategy​(boolean defaultRetriesEnabled)
        If the credentials support automatic retries, creates a copy of the identity with the provided retry strategy
        Parameters:
        defaultRetriesEnabled - a flag enabling or disabling default retries
        Returns:
        GoogleCredentials with the new default retries configuration.
      • createDelegated

        public GoogleCredentials createDelegated​(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.
        Parameters:
        user - User to impersonate.
        Returns:
        GoogleCredentials with a delegated user.