问题
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.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.
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]
回答1:
I found a workaround. Actually It is not a real answer in my point of view.
I haven't pushed google service account json key in version control.
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
Modified:
- Directly pointing
GOOGLE_CREDENTIALS
which is json key and added in config vars on heroku setting - Received value as
String
instead ofResource
In my Configuration Class,
@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);
}
}
来源:https://stackoverflow.com/questions/62326500/how-to-use-google-api-credentials-on-heroku-in-spring-boot-project