I’m stuck trying to upload files to Google Drive using Java. I used to use an older version of the API, but it’s not working anymore with the latest update.
Here’s what I tried before:
DriveService driveService = new DriveService("MyDriveIntegration-v2");
driveService.setUserAuth(USER, PASS);
Now I’m getting this error:
Error: ClassNotFound - com.google.api.client.http.HttpTransport
at Main.uploadFile(Main.java:20)
Caused by: ClassNotFoundException: com.google.api.client.http.HttpTransport
at ClassLoader.findClass(Unknown Source)
at ClassLoader.loadClass(Unknown Source)
at AppClassLoader.loadClass(Unknown Source)
... 1 more
Any ideas on how to fix this or use the new API correctly? Thanks!
yo dude, i had the same issue last week. wat worked for me was using the google drive java quickstart guide. it’s pretty straightforward. just make sure u got the right dependencies and use the drive.files().create() method to upload. dont forget to set up ur credentials properly tho, that tripped me up at first
Hey there! I’ve been through the same struggle with the Google Drive API recently. It’s a bit of a pain, but I found a workaround that might help you out.
Instead of using the older DriveService directly, I switched to using the Google Drive REST API v3. It’s more flexible and easier to work with, in my experience.
First, make sure you’ve got the right dependencies. I use Gradle, so I added these to my build.gradle:
implementation ‘com.google.api-client:google-api-client:1.32.1’
implementation ‘com.google.oauth-client:google-oauth-client-jetty:1.32.1’
implementation ‘com.google.apis:google-api-services-drive:v3-rev20210725-1.32.1’
Then, for authentication, I used a service account. It’s much simpler than dealing with OAuth flows. You’ll need to create a service account key in the Google Cloud Console and download the JSON file.
Here’s a snippet of how I set it up:
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(‘path/to/your/service-account-key.json’))
.createScoped(Collections.singleton(DriveScopes.DRIVE_FILE));
Drive driveService = new Drive.Builder(new NetHttpTransport(), GsonFactory.getDefaultInstance(), credentials)
.setApplicationName(‘Your App Name’)
.build();
Hope this helps! Let me know if you need any more details.
I recently encountered a similar issue when updating my Google Drive integration. The error you’re seeing suggests you’re missing some dependencies. Make sure you’ve included the latest Google Drive API client library in your project.
Try adding these Maven dependencies to your pom.xml:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.34.1</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-drive</artifactId>
<version>v3-rev20220815-2.0.0</version>
</dependency>
After adding these, the new API usage is quite different. You’ll need to set up OAuth 2.0 credentials and use the DriveService builder. Here’s a basic example:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
clientSecrets,
Collections.singleton(DriveScopes.DRIVE_FILE))
.setDataStoreFactory(new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
Drive service = new Drive.Builder(GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName(APPLICATION_NAME)
.build();
This should get you started with the new API. Let me know if you need more details!