0

I'm using Google Calendar client API and trying to authenticate with Google.
Following code is working in standalone application and not working in web application environment.
In case of web application,
setServiceAccountPrivateKeyFromP12File(File) method is not able to find the path what i provided, I tried in many ways of answers given by stackexchange users.
But for me no use.Everytime it is showing only one message, File path (no such file or path). Don't know what's wrong. Please help me to find a solution to this problem.

GoogleCredential credential =   new GoogleCredential.Builder().setTransport(httpTransport)  
.setJsonFactory(JSON_FACTORY) 
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Collections.singleton(CalendarScopes.CALENDAR))  
.setServiceAccountPrivateKeyFromP12File(new File(CLIENT_SECRET)).build();  

Here the private static final String CLIENT_SECRET="credentials/Test.p12";
I tried with ClassLoaders,URL but no use. always throwing the FileNotFoundException.
My project is maven project and the code is in dependent jar file.

Mr.Chowdary
  • 3,389
  • 9
  • 42
  • 66

1 Answers1

0

This is not the problem with Google Client API. In web application, we should give absolute path to create a File instance.
Generally web application gives relative path, it is not possible to create File instace with this relative path in Java. Because java.io.File doesn't know about relative paths. it knows only OS specific absolute paths to create instance of File so for that,String absoluteDiskPath = servletContext.getRealPath("/credentials/secret.p12");
This is working as expected. Thanks for help in comments.

Mr.Chowdary
  • 3,389
  • 9
  • 42
  • 66