/** * Initializes OAuth2 application default credentials based on an inputStream. * * @param inputStream a {@link java.io.InputStream} object. * @return a {@link com.google.auth.Credentials} object. * @throws java.io.IOException if any. */ public static Credentials getInputStreamCredential(InputStream inputStream) throws IOException { GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream, getHttpTransportFactory()); if (credentials instanceof ServiceAccountCredentials) { return getJwtToken((ServiceAccountCredentials)credentials); } return credentials.createScoped(CLOUD_BIGTABLE_ALL_SCOPES); }
switch (options.getCredentialType()) { case DefaultCredentials: return getApplicationDefaultCredential(); case P12: P12CredentialOptions p12Options = (P12CredentialOptions) options; return getCredentialFromPrivateKeyServiceAccount(p12Options.getServiceAccount(), p12Options.getKeyFile()); case SuppliedCredentials: if (jsonCredentialsOptions.getCachedCredentials() == null) { jsonCredentialsOptions .setCachedCredentails(getInputStreamCredential(jsonCredentialsOptions .getInputStream()));
Credentials credentials = CredentialFactory.getCredentials(credentialOptions);
/** * Initializes OAuth2 credential from a private keyfile, as described in * <a href="https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#service_accounts" * >Service accounts</a>. * * @param serviceAccountEmail Email address of the service account associated with the keyfile. * @param privateKeyFile Full local path to private keyfile. * @param scopes List of well-formed desired scopes to use with the credential. * @return a {@link com.google.auth.Credentials} object. * @throws java.io.IOException if any. * @throws java.security.GeneralSecurityException if any. */ public static Credentials getCredentialFromPrivateKeyServiceAccount( String serviceAccountEmail, String privateKeyFile, List<String> scopes) throws IOException, GeneralSecurityException { PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), new FileInputStream(privateKeyFile), "notasecret", "privatekey", "notasecret"); // Since the user specified scopes, we can't use JWT tokens return ServiceAccountCredentials.newBuilder() .setClientEmail(serviceAccountEmail) .setPrivateKey(privateKey) .setScopes(scopes) .setHttpTransportFactory(getHttpTransportFactory()) .build(); }
System.out.println(String.format("Data host: %s", options.getDataHost())); Credentials credentials = CredentialFactory.getCredentials(options.getCredentialOptions()); try { System.out.println("Attempting credential refresh...");
System.out.println(String.format("Data host: %s", options.getDataHost())); Credentials credentials = CredentialFactory.getCredentials(options.getCredentialOptions()); try { System.out.println("Attempting credential refresh...");
/** * Initializes OAuth2 application default credentials based on the environment the code is running * in. If a service account is to be used with JSON file, set the environment variable with name * "GOOGLE_APPLICATION_CREDENTIALS" to the JSON file path. For more details on application default * credentials: * <a href="https://developers.google.com/identity/protocols/application-default-credentials" > * Application Default Credentials</a>. * * @return a {@link com.google.auth.Credentials} object. * @throws java.io.IOException if any. */ public static Credentials getApplicationDefaultCredential() throws IOException { GoogleCredentials credentials = GoogleCredentials .getApplicationDefault(getHttpTransportFactory()); if (credentials instanceof ServiceAccountCredentials) { return getJwtToken((ServiceAccountCredentials)credentials); } return credentials.createScoped(CLOUD_BIGTABLE_ALL_SCOPES); }