我在使用Java将本地文件从硬盘驱动器写入Google云端硬盘时遇到问题。最终目标是将应用程序放在GAE上并直接写入Google云端硬盘。根据Drive API的文献,我有以下结构:
package com.d2d.googledrive;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
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.ParentReference;
public class GDFileInsert {
private static HttpTransport httpTransport;
private static String APPLICATION_NAME = "my_app_name";
private static String USER_EMAIL_ADDRESS = "username@gmail.com";
private static JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static String SERVICE_ACCOUNT_EMAIL = "account-1@my_app_name.iam.gserviceaccount.com";
public static void main(String[] args) throws IOException, GeneralSecurityException {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountUser(USER_EMAIL_ADDRESS)
.setServiceAccountScopes(DriveScopes.all())
.setServiceAccountPrivateKeyFromP12File(
new java.io.File("Resources/d2dloads-8d4579d34d46.p12"))
.build();
Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, null)
.setHttpRequestInitializer(credential).setApplicationName(APPLICATION_NAME).build();
// File's metadata.
File body = new File();
body.setTitle("My File Title");
body.setDescription("My File Description");
body.setMimeType("text/plain");
String parentId = "ParentDir";
// Set the parent folder.
if (parentId != null && parentId.length() > 0) {
body.setParents(
Arrays.asList(new ParentReference().setId(parentId)));
}
String filename = "Resources/myfile.txt";
// File's content.
java.io.File fileContent = new java.io.File(filename);
FileContent mediaContent = new FileContent("text/plain", fileContent);
try {
File file = service.files().insert(body, mediaContent).execute();
// Uncomment the following line to print the File ID.
System.out.println("Inserted File ID: " + file.getId());
} catch (IOException e) {
System.out.println("An error occured: " + e);
}
}
}
当我运行程序时,它给出了如下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/api/services/drive/DriveScopes
at com.d2d.googledrive.GDFileInsert.main(GDFileInsert.java:36)
基本上是" .setServiceAccountScopes(DriveScopes.all())"。我尝试过将API_KEY用于其他选项的各种选项,但没有成功。我确实为我的项目激活了Google Drive API,还创建了服务帐户密钥,API密钥和OAuth2密钥。
我不确定我做错了什么。任何指导都会有所帮助我还附上了WEB-INF / Lib jars图片
编辑2:
@ Konqi的建议适用于上述问题。现在还有另一个错误NullPointerException @" File file = service.files()。insert(body,mediaContent).execute()"。我已经定义了需要上传的文件。任何指导或建议。
Exception in thread "main" java.lang.NullPointerException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:268)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:545)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:562)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:519)
at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:384)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:418)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.d2d.googledrive.GDFileInsert.main(GDFileInsert.java:69)