private static Credential buildCredentialFromJson(String privateKeyPath, Optional<String> fsUri, HttpTransport transport, Collection<String> serviceAccountScopes) throws IOException { FileSystem fs = getFileSystem(fsUri); Path keyPath = getPrivateKey(fs, privateKeyPath); return GoogleCredential.fromStream(fs.open(keyPath), transport, JSON_FACTORY) .createScoped(serviceAccountScopes); }
@Override protected GoogleCredential getCredential(HttpTransport httpTransport, JsonFactory jsonFactory, Collection<String> scopes) throws IOException { log.debug("Loading credentials for project {} from json key, with scopes {}.", getProject(), scopes); InputStream credentialStream = new ByteArrayInputStream(jsonKey.getBytes("UTF-8")); return GoogleCredential.fromStream(credentialStream, httpTransport, jsonFactory).createScoped(scopes); } }
private GoogleCredential googleCredential(String credential) { try { return GoogleCredential.fromStream(new ByteArrayInputStream(credential.getBytes(UTF_8))); } catch (IOException e) { throw new TaskExecutionException(e); } } }
public static Credential authorize(byte[] credsFile) throws IOException { return GoogleCredential.fromStream(new ByteArrayInputStream(credsFile)).createScoped(SCOPES); }
public Credential authorize() throws IOException { return GoogleCredential .fromStream(DriveHelper.class.getResourceAsStream("/client-secrets.json")) .createScoped(Collections.singleton(DriveScopes.DRIVE)); }
private GoogleCredential loadCredential(HttpTransport transport, JsonFactory factory, String jsonPath) throws IOException { GoogleCredential credential; if (!jsonPath.isEmpty()) { FileInputStream stream = new FileInputStream(jsonPath); credential = GoogleCredential.fromStream(stream, transport, factory) .createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)); log.info("Loaded credentials from " + jsonPath); } else { log.info("Using default application credentials."); credential = GoogleCredential.getApplicationDefault(); } return credential; }
/** * @return GoogleCredential for use with Compute * @throws IOException if there was an error reading the account key */ public GoogleCredential getComputeCredential() throws IOException { if (accountKey == null) { return GoogleCredential.getApplicationDefault(); } try (InputStream is = new ByteArrayInputStream(accountKey.getBytes(StandardCharsets.UTF_8))) { return GoogleCredential.fromStream(is) .createScoped(Collections.singleton("https://www.googleapis.com/auth/cloud-platform")); } }
@Override protected GoogleCredential getCredential(HttpTransport httpTransport, JsonFactory jsonFactory, Collection<String> scopes) throws IOException { log.debug("Loading credentials for project {} from json key, with scopes {}.", getProject(), scopes); InputStream credentialStream = new ByteArrayInputStream(jsonKey.getBytes("UTF-8")); return GoogleCredential.fromStream(credentialStream, httpTransport, jsonFactory).createScoped(scopes); } }
private GoogleCredential getGoogleCredential() { try { if (StringUtils.isNotEmpty(config.getCredentialPath())) { return GoogleCredential.fromStream(new FileInputStream(config.getCredentialPath())); } else { return GoogleCredential.getApplicationDefault(); } } catch (IOException ioe) { throw new RuntimeException(ioe); } }
private GoogleCredential getGoogleCredential(GcpIamProperties gcp) throws IOException { GcpCredentials credentialProperties = gcp.getCredentials(); if (credentialProperties.getLocation() != null) { return GoogleCredential.fromStream(credentialProperties.getLocation() .getInputStream()); } if (StringUtils.hasText(credentialProperties.getEncodedKey())) { return GoogleCredential.fromStream(new ByteArrayInputStream(Base64 .getDecoder().decode(credentialProperties.getEncodedKey()))); } return GoogleCredential.getApplicationDefault(); }
private GoogleCredential getGoogleCredential(GcpIamProperties gcp) throws IOException { GcpCredentials credentialProperties = gcp.getCredentials(); if (credentialProperties.getLocation() != null) { return GoogleCredential.fromStream(credentialProperties.getLocation() .getInputStream()); } if (StringUtils.hasText(credentialProperties.getEncodedKey())) { return GoogleCredential.fromStream(new ByteArrayInputStream(Base64 .getDecoder().decode(credentialProperties.getEncodedKey()))); } return GoogleCredential.getApplicationDefault(); }
private static Credential authorizeWithServiceAccount(AppContext appContext) { if (StringUtils.isEmpty(appContext.getPrivateKeyJsonFilePath())) { throw new RuntimeException("The private key json file path is required"); } try { InputStream serviceAccountStream = new FileInputStream(appContext.getPrivateKeyJsonFilePath()); GoogleCredential credential = GoogleCredential.fromStream(serviceAccountStream, HTTP_TRANSPORT, JSON_FACTORY); return credential.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)); } catch (IOException e) { throw new RuntimeException(e); } }
private GoogleCredential getGoogleCredential() { try { if (StringUtils.isNotEmpty(config.getCredentialPath())) { return GoogleCredential.fromStream(new FileInputStream(config.getCredentialPath())); } else { return GoogleCredential.getApplicationDefault(); } } catch (IOException ioe) { throw new RuntimeException(ioe); } }
private GoogleCredential createFromFile() throws Exception { GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(credentialsFileLocation)); if (credential.createScopedRequired()) { credential = credential.createScoped(PubsubScopes.all()); } return credential; }
private static Credential buildCredentialFromJson(String privateKeyPath, Optional<String> fsUri, HttpTransport transport, Collection<String> serviceAccountScopes) throws IOException { FileSystem fs = getFileSystem(fsUri); Path keyPath = getPrivateKey(fs, privateKeyPath); return GoogleCredential.fromStream(fs.open(keyPath), transport, JSON_FACTORY) .createScoped(serviceAccountScopes); }
private GoogleCredential createFromFile() throws Exception { GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(credentialsFileLocation)); if (credential.createScopedRequired()) { credential = credential.createScoped(BigqueryScopes.all()); } return credential; }
public static AndroidPublisher init(Context context, String fileName) throws IOException { Preconditions.checkArgument(!Strings.isNullOrEmpty(context.getPackageName()), "applicationId cannot be null or empty!"); newTrustedTransport(); GoogleCredential credential = GoogleCredential.fromStream(context.getAssets().open(fileName), HTTP_TRANSPORT, JSON_FACTORY); credential = credential.createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)); return new AndroidPublisher.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(context.getPackageName()) .build(); }
/** * {@link Beta} <br/> * Return a credential defined by a Json file. * * @param credentialStream the stream with the credential definition. * @return the credential defined by the credentialStream. * @throws IOException if the credential cannot be created from the stream. */ @Beta public static GoogleCredential fromStream(InputStream credentialStream) throws IOException { return fromStream( credentialStream, Utils.getDefaultTransport(), Utils.getDefaultJsonFactory()); }
private Storage createClient() throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential.fromStream( new ByteArrayInputStream(settings.serviceAccount.getBytes(StandardCharsets.UTF_8))) .createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM)); NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); return new Storage.Builder( httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME) .setHttpRequestInitializer(credential).build(); } }
private Compute createApi(GoogleComputeProvider provider) throws IOException, GeneralSecurityException { GoogleCredential credential = GoogleCredential .fromStream(new ByteArrayInputStream(provider.gcAccount.serviceAccount.getBytes(StandardCharsets.UTF_8))) .createScoped(Collections.singleton(ComputeScopes.COMPUTE)); NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); return new Compute.Builder( httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME) .setHttpRequestInitializer(credential).build(); }