I am making a java command line application packaged in a single JAR file that uses some of the Google APIs.
I need to configure the GoogleCredential object from the private key "Credentials.p12".
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("xxxxx@developer.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_GROUP, DirectoryScopes.ADMIN_DIRECTORY_USER, DirectoryScopes.ADMIN_DIRECTORY_ORGUNIT))
.setServiceAccountUser(emailAccount)
.setServiceAccountPrivateKeyFromP12File(new File("Credentials.p12"))
.build();
directory = new Directory.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("My Cmd App")
.build();
Now I was able to get it to work, but the file "Credentials.p12" is outside of the packed JAR file.
How do I get it to work using the p12 file from the JAR?
From this documentation, the only alternative I can use is a variant of the method that uses the PrivateKey object. I am considering using InputStream to get the p12 file from the classpath:
InputStream is = this.getClass().getResourceAsStream("Credentials.p12");
I absolutely do not know how to do this.
, , Google OAuth2. , , , : Credential.p12 JAR - .