0

Good morning. My program wants to communicate with my google drive account. So I took example to see how I can do it. But I have the same error still now. Please help me.

The problem is on this line :

NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 

When I'm execute I have this error:

com.google.api.client.http.javanet.NetHttpTransport.(Lcom/google/api/client/http/javanet/ConnectionFactory;Ljavax/net/ssl/SSLSocketFactory;Ljavax/net/ssl/HostnameVerifier;)

My source code:

import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.util.store.FileDataStoreFactory; import com.google.api.services.drive.Drive; import com.google.api.services.drive.DriveScopes; import com.google.api.services.drive.model.File; import com.google.api.services.drive.model.FileList; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; public class DriveQuickstart { static final String oz = org.apache.http.conn.ssl .SSLConnectionSocketFactory.class.getProtectionDomain (). getCodeSource(). getLocation (). getPath (); private static final String APPLICATION_NAME = "DriveQuickstart"; private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); // Directory to store user credentials for this application. private static final java.io.File CREDENTIALS_FOLDER // = new java.io.File(System.getProperty("user.home"), "credentials"); private static final String CLIENT_SECRET_FILE_NAME = "client_secret.json"; // // Global instance of the scopes required by this quickstart. If modifying these // scopes, delete your previously saved credentials/ folder. // private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE); private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException { java.io.File clientSecretFilePath = new java.io.File(CREDENTIALS_FOLDER, CLIENT_SECRET_FILE_NAME); if (!clientSecretFilePath.exists()) { throw new FileNotFoundException("Please copy " + CLIENT_SECRET_FILE_NAME // + " to folder: " + CREDENTIALS_FOLDER.getAbsolutePath()); } // Load client secrets. InputStream in = new FileInputStream(clientSecretFilePath); 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(CREDENTIALS_FOLDER)) .setAccessType("offline").build(); return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); } public static void main(String... args) throws IOException, GeneralSecurityException { System.out.println("voici le path "+oz); System.out.println("CREDENTIALS_FOLDER: " + CREDENTIALS_FOLDER.getAbsolutePath()); // 1: Create CREDENTIALS_FOLDER if (!CREDENTIALS_FOLDER.exists()) { CREDENTIALS_FOLDER.mkdirs(); System.out.println("Created Folder: " + CREDENTIALS_FOLDER.getAbsolutePath()); System.out.println("Copy file " + CLIENT_SECRET_FILE_NAME + " into folder above.. and rerun this class!!"); return; } // 2: Build a new authorized API client service. NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); // 3: Read client_secret.json file & create Credential object. Credential credential = getCredentials(HTTP_TRANSPORT); // 5: Create Google Drive Service. Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) // .setApplicationName(APPLICATION_NAME).build(); // Print the names and IDs for up to 10 files. FileList result = service.files().list().setPageSize(10).setFields("nextPageToken, files(id, name)").execute(); List<File> files = result.getFiles(); if (files == null || files.isEmpty()) { System.out.println("No files found."); } else { System.out.println("Files:"); for (File file : files) { System.out.printf("%s (%s)\n", file.getName(), file.getId()); } } } } 
4
  • show your pom.xml or dependencies version Commented Mar 7, 2020 at 9:07
  • pls add the required jar in lib folder Commented Mar 7, 2020 at 9:09
  • i don't have a pom.xml, i don't use marven. I've add all required jar in my lib folder. Please, what are you talking about?? I need Help please Commented Mar 8, 2020 at 16:16
  • @shihabudheenk please which jar are you talking about?? Commented Mar 10, 2020 at 7:20

2 Answers 2

1

This is a jar dependency issue. Update your question with a screenshot of libs folder.

In order to work this please add the below jars in lib if not exists already

google-oauth-client-1.30.5.jar google-oauth-client-java6-1.30.6.jar google-oauth-client-jetty-1.30.6.jar google-api-client-1.30.9.jar google-http-client-1.34.0.jar google-http-client-jackson2-1.34.2.jar google-api-services-drive-v3-rev195-1.25.0.jar httpclient-4.5.12.jar 

Also, add the below lines of code just above where the exception is occurring to see from which jar they are referenced in

System.out.println(NetHttpTransport.class.getProtectionDomain().getCodeSource().getLocation().getPath()); System.out.println(GoogleNetHttpTransport.class.getProtectionDomain().getCodeSource().getLocation().getPath()); NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
Sign up to request clarification or add additional context in comments.

3 Comments

i've added the lines; And this is the result: /C:/Users/NGONO/Documents/NetBeansProjects/SAMAC/google-http-client-1.11.0-beta.jar /C:/Users/NGONO/Documents/NetBeansProjects/SAMAC/google-api-client-1.30.9.jar
all the jar have being added
Well I added the jars mentioned in my post and it executed without exception. I had the 1.34.0 version of google-http-client jar. Try that and remove 1.11.0-beta
0

use your class package while calling the class

HttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();

I hope it will work

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.