(Java) right location of google credential.json as the Secret Files

In the topic Using google application credentials JSON? are the information about credential.json as the Secret File.
But it does’t work with standard localization of credential.json (/src/main/resources ) with invoke this method:
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(MailboxService.class.getResourceAsStream(“/credential.json”)));
I get error:
java.lang.NullPointerException: null
at java.base/java.io.Reader.(Reader.java:168) ~[na:na]
at java.base/java.io.InputStreamReader.(InputStreamReader.java:76) ~[na:na]

Should i use the sugested /etc/secrets/<YOUR_FILE> directory or the problem is somewhere else ?

I was able to solve the issue as soon as I wrote the query. I think this thread will be helpful for other developers.

You should place credential.json in the folder:

my-spring-boot-project
|-- src
|   |-- main
|       |-- java
|           |-- com
|               |-- example
|                   |-- myapplication
|                       |-- controller
|                       |-- service
|                       |-- MyApplication.java
|-- resources
|   |-- application.properties
|-- target
|-- etc
|   |-- secrets
|       |-- credential.json
|-- ...

Then use a function similar to:

public GoogleClientSecrets loadClientSecrets(GsonFactory jsonFactory) throws IOException, GeneralSecurityException 
    {
        String fileName = "credential.json";
        String dirName = "/etc/secrets/";

        Path secretsPath = Paths.get(dirName, fileName);
        File secretFile = secretsPath.toFile();

        try (InputStream in = Files.newInputStream(secretFile.toPath())) {
            return GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));
        }
    }

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.