使用Google Drive API上传文件时遇到问题。 API不会返回任何错误,并显示文件已成功上传。但是,当我尝试检索文件列表时,当我尝试查看Google Drive API页面时,它不会显示,也不会显示。
以下是一些观察结果:
以下是我的代码的外观(我已尝试根据Java代码段创建代码)。
public class Quickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Drive API Java Quickstart";
/** Directory to store user credentials for this application. */
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/**
* Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials at
* ~/.credentials/drive-java-quickstart
*/
private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
GoogleCredential credential = GoogleCredential.fromStream(Quickstart.class.getResourceAsStream("/secret.json"));
credential = credential.createScoped(SCOPES);
return credential;
}
/**
* Build and return an authorized Drive client service.
*
* @return an authorized Drive client service
* @throws IOException
*/
public static Drive getDriveService() throws IOException {
Credential credential = authorize();
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
}
public static String generateFolder(Drive service) throws IOException {
File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = service.files().create(fileMetadata).setFields("id").execute();
System.out.println("Folder ID: " + file.getId());
return file.getId();
}
public static void uploadFile(Create createFile, Drive service) throws IOException, InterruptedException {
File file = createFile.execute();
Thread.sleep(3000);
MediaHttpUploader uploader = createFile.getMediaHttpUploader();
System.out.println(uploader.getProgress());
System.out.println("File ID: " + file.getId());
System.out.println(service.files().list());
}
public static Create createFile(String path, Drive service) throws IOException {
String folderId = generateFolder(service);
File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
fileMetadata.setParents(Collections.singletonList(folderId));
java.io.File filePath = new java.io.File(path);
FileContent mediaContent = new FileContent("image/jpeg", filePath);
Create createFile = service.files().create(fileMetadata, mediaContent).setFields("id, parents");
return createFile;
}
public static void main(String[] args) throws IOException, InterruptedException {
Drive service = getDriveService();
String path = "F:/workspace/drive/files/photo.jpg";
Create createdFile = createFile(path, service);
uploadFile(createdFile, service);
}
}
此代码的结果是:
文件夹ID:&#34;文件夹ID的字符串&#34;
1.0
文件ID:&#34;文件ID的字符串&#34;
{}
(其中{}应该是驱动器上的所有文件)
任何人都知道为什么它没有出现在驱动器上?
答案 0 :(得分:2)
您似乎使用服务帐户进行身份验证。您需要记住服务帐户不是您,将其视为虚拟用户,它拥有自己的Google云端硬盘帐户。您一直在将文件上传到服务帐户Google云端硬盘帐户。您可以执行files.list来查看这些上传的文件。服务帐户没有Web界面视图。
您可以使用服务帐户电子邮件地址并在个人Google云端硬盘帐户上共享目录,这将为服务帐户授予上传到此目录的权限。您需要记住让服务帐户修补文件以授予您的用户访问文件的权限,否则您将无权使用这些文件。