Class GoogleServiceAccountCredentials

java.lang.Object
com.codename1.backend.security.oauth2.client.credentials.GoogleServiceAccountCredentials

public final class GoogleServiceAccountCredentials extends Object

A Google service account, read from the JSON key file the Cloud console downloads, as a JwtBearerGrant. No Google library is involved: the key is the file's PKCS#8 PEM, the assertion is signed by this runtime's own RSA, and the exchange is one POST.

AccessTokenProvider storage = new AccessTokenProvider(
        GoogleServiceAccountCredentials.fromFile("/etc/app/service-account.json")
                .grant("https://www.googleapis.com/auth/devstorage.read_only"));
Web.Result listing = Web.getJson(
        "https://storage.googleapis.com/storage/v1/b/my-bucket/o",
        storage.getToken().getTokenValue());

The key file is a credential: keep it out of the repository and readable by the server's user alone. toString() names the account, never the key.

  • Field Details

    • TOKEN_URI

      public static final String TOKEN_URI
      Where Google exchanges an assertion when the file does not say.
      See Also:
  • Method Details

    • fromFile

      public static GoogleServiceAccountCredentials fromFile(String path) throws IOException
      Reads a service-account key file.
      Throws:
      IOException - when it cannot be read, or is not a service-account key
    • fromJson

      public static GoogleServiceAccountCredentials fromJson(String json) throws IOException
      Reads the contents of a service-account key file.
      Throws:
      IOException - when it is not a service-account key; the message names the field that is wrong and never repeats the key
    • getClientEmail

      public String getClientEmail()
    • getProjectId

      public String getProjectId()
    • getTokenUri

      public String getTokenUri()
    • grant

      public JwtBearerGrant grant(String... scopes)
      A grant for these scopes, acting as the service account itself.
    • grant

      public JwtBearerGrant grant(Collection<String> scopes)
      A grant for these scopes, acting as the service account itself.
    • builder

      public JwtBearerGrant.Builder builder()
      A builder preset for this account, for a subject (domain-wide delegation), a transport or a clock.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
      Overrides:
      toString in class Object