Consider the following code snippet
public static GoogleCredentials getCreds() throws IOException {
GoogleCredentials credentials = null;
String fileName = "abc.json";
ClassLoader classLoader = BigQueryService.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream(fileName);
if (inputStream == null) {
// Handle file not found scenario
try {
throw new FileNotFoundException("File " + fileName + " not found in resources folder");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
credentials = ServiceAccountCredentials.fromStream(inputStream);
return credentials;
}
abc.json
{
“type”: “service_account”,
“project_id”: “ofi-tracemark-prod”,
“private_key_id”: “de4a477f133b7b1c859e5bdf9c0feb44a07c3391”,
“private_key”: “—–BEGIN PRIVATE KEY—–nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDD1vp4oEVvvhA7n7cXXpV4HOo70FyOtu3G751+OtSLb4ixn”,
“client_email”: “[email protected]”,
“client_id”: “106171349280765698981”,
“auth_uri”: “https://accounts.google.com/o/oauth2/auth”,
“token_uri”: “https://oauth2.googleapis.com/token”,
“auth_provider_x509_cert_url”: “https://www.googleapis.com/oauth2/v1/certs”,
“client_x509_cert_url”: “https://www.googleapis.com/robot/v1/metadata/x509/ofi-bigquery%40ofi-tracemark-prod.iam.gserviceaccount.com”,
“universe_domain”: “googleapis.com”
}
Here we are getting the InputStream object by classLoader.getResourceAsStream(fileName);
then after checking the confdition , we are passing inputStream to fromStream() to get the credential object as ServiceAccountCredentials.fromStream(inputStream);
Here fromStream reads the contents of json file and returns the credential object.
Can anyone provide a solution as without creating InputStream object by providing filename to
classLoader.getResourceAsStream() we will get credential object.?
Is there any alternative way to read the contents of json file and passing those data to which method of ServiceAccountCredentials so that it will return credential object.?
For example ,
if we write json file details as key and value in the application.properties file ,
How can we use those info from properties file and sending to as parameter of some method of ServiceAccountCredentials class to get credentials object.
I tried to put those key value pair from json contents to application.properties file ,but facing issue to find which method of ServiceAccountCredentials need to call for those parameter present in json contents in application.properties file.