1

I'm using the gmail API. When I war package my application, my client_secrets.json and credentials file are not inside. How do I ensure that they are available for use?

tery
  • 183
  • 1
  • 2
  • 10

1 Answers1

3

In case you are using Maven or Gradle, you need to put them into /src/main/resources/client_secrets.json. (not into the root folder of your project!)

Then they are 'inside' the jar.

Then you can load an pass it to GoogleClientSecrets like this:

InputStream in = XXX.class.getResourceAsStream("/client_secrets.json");
GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

XXX you should replace with your class name. Google is using the same in their quickstart. See: https://developers.google.com/gmail/api/quickstart/java Step 3.

Update regarding your comment:

Everything which you place inside /src/main/resources is inside the "root" of your war-File. That means they are in the root of your classpath. All this stuff you can access over the classloader (not FileInputStream) like this:

XXX.class.getResourceAsStream("/folder/filename");

The FileInputStream which you mentioned you use normally only to access files outside the war

Your suggestion with InputStream in = new FileInputStream("src/main/resources/credentials/gmail/client_secret.json"); Does only work inside your IDE because there the folder src/main/resources really exists. When you package it as an war, as i described above, the content of src/main/resources is moved into the root of your war.

d0x
  • 11,040
  • 17
  • 69
  • 104
  • thanks! resources does package the static file. what if I want to deploy with war? does "src/main/resources/credentials/gmail/client_secret.json" still work? – tery Nov 08 '15 at 11:02
  • Yes in `war`-package mode `/src/main/resources` are also considered. Otherwise all your logging configuration and stuff like that would be inside the war file. Is that was you asked? – d0x Nov 08 '15 at 11:19
  • not exactly...how do I now reference those resources? InputStream in = new FileInputStream("src/main/resources/credentials/gmail/client_secret.json"); doesn't work any more. – tery Nov 08 '15 at 11:22
  • i updated the answer, can you check whether this makes sense for you? – d0x Nov 08 '15 at 11:29
  • makes sense, substituting stuff before I try deployment again...what do I change DATA_STORE_DIR = new File("src/main/resources/credentials/gmail"); to? – tery Nov 08 '15 at 11:37
  • Nope, `XXX.class.getResourceAsStream("/credentials/gmail/client_secret.json");` should work inside your IDE and on production. – d0x Nov 08 '15 at 11:45
  • oh, that works, thanks! for the second variable, DATA_STORE_DIR, what do I change it to? – tery Nov 08 '15 at 11:53
  • I am trying private static java.io.File DATA_STORE_DIR = new File(GmailQuickstart.class.getResource("/credentials/gmail/").toURI()); but it gives an unhandled exception, can't put a throws on the class... – tery Nov 08 '15 at 11:59
  • Stuff like this you can initialize in static constructors: http://stackoverflow.com/questions/335311/static-initializer-in-java But thats not a good style of programming – d0x Nov 08 '15 at 12:03
  • Don't forget to upvote and accept the answer if it suites you – d0x Nov 08 '15 at 12:28
  • yep thanks! it works on my local machine, but still fails on my deployed instance. I can't figure out what happened...setting logging.path = classpath:/log still doesn't output a log file :/ – tery Nov 08 '15 at 12:33
  • You can open another question for that. Give some information about your enviroment (which spring version, server, ..., log configuration) – d0x Nov 08 '15 at 12:40