@Test public void createOptionsWithFirebaseCredential() throws IOException { FirebaseOptions firebaseOptions = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(ServiceAccount.EDITOR.asStream())) .build(); assertNotNull(firebaseOptions.getJsonFactory()); assertNotNull(firebaseOptions.getHttpTransport()); assertNull(firebaseOptions.getDatabaseUrl()); assertNull(firebaseOptions.getStorageBucket()); GoogleCredentials credentials = firebaseOptions.getCredentials(); assertNotNull(credentials); assertTrue(credentials instanceof ServiceAccountCredentials); assertEquals( GoogleCredential.fromStream(ServiceAccount.EDITOR.asStream()).getServiceAccountId(), ((ServiceAccountCredentials) credentials).getClientEmail()); }
/** * Inicializace firebase služby */ private void initFirebase() { try { InputStream serviceAccount = getClass().getResourceAsStream(FIREBASE_CREDENTAILS); Map<String, Object> auth = new HashMap<>(); auth.put("uid", "my_resources"); FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) .setDatabaseUrl(FIREBASE_URL) .setDatabaseAuthVariableOverride(auth) .build(); FirebaseApp.initializeApp(options); } catch (Exception e) { logger.info("Nemůžu se připojit k firebase", e); } }
@Provides @Singleton static FirebaseApp firebaseApp(FirebaseAuthConfig config) { final FirebaseOptions options; try { options = new FirebaseOptions.Builder() .setCredential( FirebaseCredentials.fromCertificate( new ByteArrayInputStream( Base64.getDecoder().decode(config.getServiceAccountBase64())))) .setDatabaseUrl("https://" + config.getProjectId() + ".firebaseio.com") .build(); } catch (IOException e) { throw new UncheckedIOException("Could not read certificate.", e); } FirebaseApp.initializeApp(options); return FirebaseApp.getInstance(); }
/** * Initialize firebase. * * @param pathConfig the path config * @param atribute the atribute * @param pathdefault the pathdefault * @return the firebase auth */ private static void initializeFirebase(String pathConfig, String atribute, String pathdefault) { try { File config = new File(pathConfig + properties.getProperty(atribute)); InputStream serviceAccount; if (config.exists()) { serviceAccount = new FileInputStream(config); logger.info("initializeFirebase outside" + config); } else { serviceAccount = FileUtils.getFile(pathdefault); logger.info("initializeFirebase inside" + pathdefault); } // Initialize Firebase FirebaseOptions options = new FirebaseOptions.Builder().setCredential(FirebaseCredentials.fromCertificate(serviceAccount)).build(); FirebaseApp.initializeApp(options, "members"); serviceAccount.close(); } catch (Exception e) { logger.error("initializeFirebase ", e); } }
private FirebaseCredential getCreds(String credsPath) throws IOException { if (StringUtils.isNotBlank(credsPath)) { return FirebaseCredentials.fromCertificate(new FileInputStream(credsPath)); } else { return FirebaseCredentials.applicationDefault(); } }
private FirebaseApp initialize() { try (FileInputStream serviceAccount = new FileInputStream(getServiceAccountFilePath())) { FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) .setDatabaseUrl(databaseUrl) .build(); return FirebaseApp.initializeApp(options); } catch (IOException e) { System.out.println(e.getMessage()); return null; } }
public void initFirebase(String databaseUrl, InputStream serviceAccount) { FirebaseOptions options = new FirebaseOptions.Builder() .setCredential(FirebaseCredentials.fromCertificate(serviceAccount)) .setDatabaseUrl(databaseUrl) .build(); if (FirebaseApp.getApps().isEmpty()) { FirebaseApp.initializeApp(options); } }
public String getAccessToken(InputStream serviceAccount) { Task<String> tokenTask = FirebaseCredentials.fromCertificate(serviceAccount) .getAccessToken(false); try { Tasks.await(tokenTask); } catch (InterruptedException | ExecutionException e) { LOG.log(Level.SEVERE, "An error occurred while generating the access token", e); return null; } return tokenTask.getResult(); }