【问题标题】:How to use Google API credentials on Heroku in Spring Boot Project如何在 Spring Boot Project 中的 Heroku 上使用 Google API 凭据
【发布时间】:2020-06-11 14:17:44
【问题描述】:

我遵循official 文档来实现这一点。但我仍然面临问题。我正在做春季启动项目。下面是我的代码,

@Configuration
public class FirebaseConfiguration {

   // database url, other urls
   // other codes...

   @Value(value = "classpath:google-credentials.json")
   private Resource gservicesConfig;

  @Bean
  public FirebaseApp provideFirebaseOptions() throws IOException {
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream((gservicesConfig.getInputStream())))
        .setDatabaseUrl(databaseUrl)
        .setStorageBucket(storageUrl)
        .build();

    return FirebaseApp.initializeApp(options);
  }

}

我还按照以下官方文档添加了配置

1.创建 Config Vars 键 GOOGLE_CREDENTIALS 并按原样粘贴服务帐户凭据 JSON 文件的内容。 2.在 Config Vars GOOGLE_APPLICATION_CREDENTIALS 下创建一个键,并将值设置为 google-credentials.json。

还添加了https://github.com/elishaterada/heroku-google-application-credentials-buildpack.git buildpack。但在日志中我遇到了以下问题。

Caused by: java.io.FileNotFoundException: class path resource [google-credentials.json] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
at com.read.bible.service.config.FirebaseConfiguration.provideFirebaseOptions(FirebaseConfiguration.java:35) ~[classes!/:na]
at com.read.bible.service.config.FirebaseConfiguration$$EnhancerBySpringCGLIB$$93cb42d.CGLIB$provideFirebaseOptions$0(<generated>) ~[classes!/:na]

【问题讨论】:

    标签: firebase spring-boot heroku google-api


    【解决方案1】:

    我找到了解决方法。实际上,在我看来,这不是一个真正的答案。

    我没有在版本控制中推送 google 服务帐号 json 密钥。

    我只遵循了official heroku 文档提供的第一步(从配置变量中删除了GOOGLE_APPLICATION_CREDENTIALS),并在我的配置中进行了如下修改

    修改:

    1. 直接指向GOOGLE_CREDENTIALS 这是json键并添加到heroku设置的配置变量中
    2. 接收值为String 而不是Resource

    在我的配置类中,

    @Configuration
    public class FirebaseConfiguration {
    
       // database url, other urls
       // other codes...
    
       @Value("${GOOGLE_CREDENTIALS}")
       private String gservicesConfig;
    
      @Bean
      public FirebaseApp provideFirebaseOptions() throws IOException {
        JSONObject jsonObject = new JSONObject(gservicesConfig.toString());
        InputStream is = new ByteArrayInputStream(jsonObject.toString().getBytes());
        FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream((is)))
            .setDatabaseUrl(databaseUrl)
            .setStorageBucket(storageUrl)
            .build();
    
        return FirebaseApp.initializeApp(options);
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 2021-01-18
      • 2020-01-31
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      相关资源
      最近更新 更多