@Override public GoogleCredentials getGoogleCredentials(Map<PropertyDescriptor, String> properties, HttpTransportFactory transportFactory) throws IOException { return GoogleCredentials.fromStream(getServiceAccountJson(properties), transportFactory); }
/** * Constructs a ContainerRegistryAuthSupplier for the account with the given credentials. * * @see Builder */ public static Builder fromStream(final InputStream credentialsStream) throws IOException { final GoogleCredentials credentials = GoogleCredentials.fromStream(credentialsStream); return new Builder(credentials); }
private GoogleCredentials loadGoogleCredentials( final boolean useDefaultCredentials, final File credentialsFile) throws ArgumentParserException, IOException { if (useDefaultCredentials && credentialsFile != null) { final String msg = String.format("Cannot set both %s and %s", this.googleCloudCredentialsFile.getDest(), this.useGoogleDefaultApplicationCredentials.getDest()); throw new IllegalArgumentException(msg); } if (useDefaultCredentials) { return GoogleCredentials.getApplicationDefault(); } try (final FileInputStream stream = new FileInputStream(credentialsFile)) { return GoogleCredentials.fromStream(stream); } }
if (file.exists()) { try (final FileInputStream s = new FileInputStream(file)) { credentials = GoogleCredentials.fromStream(s); LOG.info("Using Google Credentials from file: " + file.getAbsolutePath());
if (file.exists()) { try (FileInputStream inputStream = new FileInputStream(file)) { credentials = GoogleCredentials.fromStream(inputStream); getLog().info("Using Google credentials from file: " + file.getAbsolutePath());
TranslateOptions translateOptions = TranslateOptions.newBuilder() .setCredentials(GoogleCredentials.fromStream(keyStream)) .setProjectId(projectId) .setRetrySettings(retryParams())
StorageOptions storageOptions = StorageOptions.newBuilder() .setCredentials(GoogleCredentials.fromStream(keyStream)) .setProjectId(projectId) .setRetrySettings(retrySettings())
public static GoogleCredentials loadCredentials(S3UploadMetadata uploadMetadata, JsonObjectFileHelper jsonHelper) { try { if (!uploadMetadata.getGcsCredentials().isEmpty()) { return GoogleCredentials.fromStream(jsonHelper.toInputStream(uploadMetadata.getGcsCredentials())); } // Load from default credentials as determined by GOOGLE_APPLICATION_CREDENTIALS var if none provided in metadata return GoogleCredentials.getApplicationDefault(); } catch (IOException e) { throw new RuntimeException("Issue reading credentials file specified in `GOOGLE_APPLICATION_CREDENTIALS`", e); } }
static void authExplicit(String jsonPath) throws IOException { // You can specify a credential file by providing a path to GoogleCredentials. // Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable. GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath)) .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform")); Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService(); System.out.println("Buckets:"); Page<Bucket> buckets = storage.list(); for (Bucket bucket : buckets.iterateAll()) { System.out.println(bucket.toString()); } } // [END auth_cloud_explicit]
/** * Returns credentials defined by a JSON file stream. * * <p>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.</p> * * @param credentialsStream the stream with the credential definition. * @return the credential defined by the credentialsStream. * @throws IOException if the credential cannot be created from the stream. **/ public static GoogleCredentials fromStream(InputStream credentialsStream) throws IOException { return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY); }
/** * Returns credentials defined by a JSON file stream. * * <p>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.</p> * * @param credentialsStream the stream with the credential definition. * @return the credential defined by the credentialsStream. * @throws IOException if the credential cannot be created from the stream. **/ public static GoogleCredentials fromStream(InputStream credentialsStream) throws IOException { return fromStream(credentialsStream, OAuth2Utils.HTTP_TRANSPORT_FACTORY); }
public void openCredentials(File jsonKey) throws InvalidGoogleCredentialsException, IOException { credentialsFile = jsonKey; projectId = readProjectId(jsonKey); try ( FileInputStream input = new FileInputStream(jsonKey); ) { credentials = GoogleCredentials.fromStream(input); } }
/** * @return GoogleCredentials for use with Dataproc * @throws IOException if there was an error reading the account key */ public GoogleCredentials getDataprocCredentials() throws IOException { if (accountKey == null) { return getComputeEngineCredentials(); } try (InputStream is = new ByteArrayInputStream(accountKey.getBytes(StandardCharsets.UTF_8))) { return GoogleCredentials.fromStream(is); } }
@Override public Credentials getCredential() throws IOException, GeneralSecurityException { return GoogleCredentials.fromStream(new FileInputStream(options.getServiceAccountFile())).createScoped(SCOPES); } }
@Override public Credentials getCredential() throws IOException, GeneralSecurityException { return GoogleCredentials.fromStream(new FileInputStream(options.getServiceAccountFile())).createScoped(SCOPES); } }
@Provides @Singleton Credentials provideCredentials(@ClientSecretsFile String clientSecretsFile, @Scopes List<String> scopes) throws FileNotFoundException, IOException { return GoogleCredentials .fromStream(new FileInputStream(clientSecretsFile)) .createScoped(scopes); } }
public static Credentials createCredentials(BigQueryDatastoreProperties datastore) { try { GoogleCredentials credential = GoogleCredentials .fromStream(new FileInputStream(datastore.serviceAccountFile.getValue())).createScoped(BigqueryScopes.all()); return credential; } catch (IOException e) { throw new RuntimeException("Exception when read service account file: " + datastore.serviceAccountFile.getValue() + "\nMessage is:" + e.getMessage()); } }
public static Credentials createCredentials(BigQueryDatastoreProperties datastore) { try { GoogleCredentials credential = GoogleCredentials .fromStream(new FileInputStream(datastore.serviceAccountFile.getValue())).createScoped(BigqueryScopes.all()); return credential; } catch (IOException e) { throw new RuntimeException("Exception when read service account file: " + datastore.serviceAccountFile.getValue() + "\nMessage is:" + e.getMessage()); } }
private static void testFromStreamException(InputStream stream, String expectedMessageContent) { try { GoogleCredentials.fromStream(stream, DUMMY_TRANSPORT_FACTORY); fail(String.format("Should throw exception with message containing '%s'", expectedMessageContent)); } catch (IOException expected) { assertTrue(expected.getMessage().contains(expectedMessageContent)); } } }
@Test public void fromStream_user_providesToken() throws IOException { MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory(); transportFactory.transport.addClient(USER_CLIENT_ID, USER_CLIENT_SECRET); transportFactory.transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN); InputStream userStream = UserCredentialsTest.writeUserStream(USER_CLIENT_ID, USER_CLIENT_SECRET, REFRESH_TOKEN); GoogleCredentials credentials = GoogleCredentials.fromStream(userStream, transportFactory); assertNotNull(credentials); Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI); TestUtils.assertContainsBearerToken(metadata, ACCESS_TOKEN); }