Thank you very much for reading this post, any help will be greatly appreciated!!
I am trying to use service account to list and create a text file in the Drive. Have generated a Service account, downloaded the p12 file also enabled Drive api for the account. But I am getting SSLHandshakeException.
Googleapi version: libs-sources/google-api-services-drive-v2-rev168-1.20.0-sources.jar
java version: "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
Have also loaded the downloaded p12 file to cacerts file under \jdk1.7.0_79\jre\lib\security.
An error occurred: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Unknown Source) ........... Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.validator.PKIXValidator.doBuild(Unknown Source) ................ Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
Here is my source code:
private static void getSomethingFromDrive() throws Exception {
String emailAddress ="XXXXXtest@XXXXX.iam.gserviceaccount.com";
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential.Builder credBuilder = new GoogleCredential.Builder();
credBuilder.setTransport(httpTransport);
credBuilder.setJsonFactory(JSON_FACTORY);
credBuilder.setServiceAccountId(emailAddress);
credBuilder.setServiceAccountPrivateKeyFromP12File(new File("XXXXX.p12"));
credBuilder.setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE));
GoogleCredential credential = credBuilder.build();
Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, credential).build();
DriveOperation.printAbout(service);
//Insert a file
com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle("My document");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("C:/Personal/document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());
}