【问题标题】:String to FileInputSteam字符串到 FileInputStream
【发布时间】:2020-07-23 07:26:55
【问题描述】:
FileInputStream serviceAccount =
    new FileInputStream("push-manager-app/src/main/resources/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://test1-use.firebaseio.com")
                .build();
FirebaseApp.initializeApp(options);

Firebase 连接器的上述代码工作正常。但是在openshift 环境中,我不能在资源中提交serviceAccountKey.json,因为不同的环境会有所不同。我可以在每个环境中从openshift configmap 中将其作为String 获取。

为了运行这段代码,我需要将String 转换为FileInputStream。我不知道该怎么做。我有一种解决方法,我可以读取String,生成文件并使用它。但我想使用正确的方法。我检查了Firebase API 的其他选项,但它甚至无法处理InputStream

【问题讨论】:

    标签: java spring-boot push-notification firebase-cloud-messaging openshift


    【解决方案1】:
    String json = "xxxxxx";
    InputStream stream = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
    
    

    【讨论】:

    • 这就是我尝试过的,但 InputStream 不适用于 GoogleCredential api
    • 你能将字符串写入openshift 环境中的临时文件吗?
    • @Pramod 不是这个库吗?似乎接受 InputStream? googleapis.dev/java/google-auth-library/latest/index.html?com/…
    • 抱歉,它成功了。我想我上次错过了字符集。感谢您提供通知。
    【解决方案2】:
    private static InputStream getFile() {
        return FireBaseService.class.getResourceAsStream("/dataCred.json");
    }
    private static final String DATABASE_URL = "DATABASE_URL";
    
    public static void initiainzeSDK() {
        
       try {
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(getFile()))
                    .setDatabaseUrl(DATABASE_URL)
                    .build();
    
        FirebaseApp.initializeApp(options);
        // Initialize the default app
    
        } catch (IOException e) {
           System.out.println(e.getMessage());
       }
    }
    

    确保你的 json 文件没有语法错误

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 2010-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多