1

I try to implement the code of GmailAPI, but the code doesn't find the credentials.jar. I put a file.exists() verification inside of the google default code, and I find them on that location. So why I see the File Not Found error in the executation ?

Here is the google default code with my file verification:

private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        //Verify if the directory of credentials exists:
        File credentials = new File(CREDENTIALS_FILE_PATH);
        if(credentials.exists()){
            System.out.print("The credentials directory exists! --> "+credentials.getAbsolutePath());
        }
        // Load client secrets.
        InputStream in = GmailAPI.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

And here is the output of execution:

The credentials directory exists! --> C:\rfp\GymSoft\Google\credentials.json
Exception in thread "main" java.io.FileNotFoundException: Resource not found: Google/credentials.json
    at apoio.GmailAPI.getCredentials(GmailAPI.java:43)
    at apoio.GmailAPI.main(GmailAPI.java:61)
C:\rfp\GymSoft\nbproject\build-impl.xml:1340: The following error occurred while executing this line:
C:\rfp\GymSoft\nbproject\build-impl.xml:981: Java returned: 1
BUILD FAILED (total time: 1 second)
  • Google/credentials.json is not the same as C:\rfp\GymSoft\Google\credentials.json. print the entire path in your errorµ – Stultuske Nov 06 '19 at 08:14
  • It seems simply file handling issue, nothing specific to GmailAPI. You already create a `File` object to check whether the file exists. Use his object to open the stream as well. eg: `GmailAPI.class.getResourceAsStream(credentials.getAbsolutePath)` – Balázs Nemes Nov 06 '19 at 09:10

1 Answers1

1

You must put your credentials.json file in your project's resources folder, then just declare a constant and implement it in your code:

private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
InputStream in = GmailAPI.class.getResourceAsStream(CREDENTIALS_FILE_PATH);

By default, That is the place where GmailAPI searches the credentials.json file unless you set an absolute path, but to avoid troubles it's better to simply put it there in the resources folder.

You can check a quickstart Gmail API example using Java here.

alberto vielma
  • 2,302
  • 2
  • 8
  • 15