如何在Spring Boot Project中的Heroku上使用Google API凭据 [英] How to use Google API credentials on Heroku in Spring Boot Project

查看:46
本文介绍了如何在Spring Boot Project中的Heroku上使用Google API凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已遵循官方文档来实现这.但是我仍然面临问题.我正在做Spring Boot项目.下面是我的代码,

I have followed official documentation to achieve this. But I'm still facing issue. I'm doing spring boot project. below is my code,

@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);
  }

}

我还根据以下官方文档添加了配置

Also i have added Configs as per official documentation as below

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

1.Create Config Vars key GOOGLE_CREDENTIALS and paste the content of service account credential JSON file as is. 2.Create a key under Config Vars GOOGLE_APPLICATION_CREDENTIALS and set a value as google-credentials.json.

还添加了 https://github.com/elishaterada/heroku-google-application-credentials-buildpack.git buildpack.但是在日志中,即时通讯出现了问题.

Added https://github.com/elishaterada/heroku-google-application-credentials-buildpack.git buildpack as well. But in logs im getting below issue.

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]

推荐答案

我找到了一种解决方法.实际上,从我的角度来看,这不是一个真正的答案.

I found a workaround. Actually It is not a real answer in my point of view.

我没有在版本控制中按google服务帐户json键.

I haven't pushed google service account json key in version control.

我只遵循了第一步(从配置变量中删除了 GOOGLE_APPLICATION_CREDENTIALS ),该步骤由

I have followed only the first step (Removed GOOGLE_APPLICATION_CREDENTIALS from config vars) which was provided by official heroku document and modifed in my configuration as below

已修改:

  1. 直接指向 GOOGLE_CREDENTIALS ,它是json键,并在heroku设置中添加到配置变量中
  2. String 而不是 Resource
  3. 的形式接收到的值
  1. Directly pointing GOOGLE_CREDENTIALS which is json key and added in config vars on heroku setting
  2. Received value as String instead of 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);
  }

}

这篇关于如何在Spring Boot Project中的Heroku上使用Google API凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆