使用 Google 驱动器 API 时,上传的文件不显示
Uploaded file does not show when using Google Drive API
我在使用 Google Drive API 上传文件时遇到一些问题。 API 不会 return 任何错误并显示文件已成功上传。但是,当我尝试检索文件列表时,它没有出现,当我尝试查看 Google Drive API 页面时也没有出现。
以下是一些观察结果:
- 我已经检查过 Java 上是否存在该路径并且确实存在。
- 文件上传成功,因为是returnID,进度为1.0,无消息错误
- 我使用的范围是驱动器全局范围,而不是文件范围。 “https://www.googleapis.com/auth/drive”
- 查询成功出现在 Google 的控制台开发者页面上。
- 我已经尝试使用 root 作为文件夹而不是创建文件夹来创建它。
- 本项目使用的Api版本是"v3-rev90-1.23.0"
这是我的代码的样子(我尝试根据 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:"String of the folder ID"
1.0
文件编号:"String of the file ID"
{}
(其中 {} 应该是驱动器上存在的所有文件)
有人知道为什么它没有出现在驱动器上吗?
您似乎是在使用服务帐户进行身份验证。您需要记住服务帐户不是您,将其视为虚拟用户,它有自己的 Google 驱动器帐户。您一直在将文件上传到服务帐户 Google 驱动器帐户。您可以执行 files.list 来查看这些上传的文件。服务帐户没有 Web 界面视图。
您可以获取服务帐户电子邮件地址并在您的个人 google 驱动器帐户上共享一个目录,这将授予服务帐户上传到该目录的权限。您需要记住让服务帐户修补文件以授予您的用户访问这些文件的权限,否则您将没有任何权限。
我在使用 Google Drive API 上传文件时遇到一些问题。 API 不会 return 任何错误并显示文件已成功上传。但是,当我尝试检索文件列表时,它没有出现,当我尝试查看 Google Drive API 页面时也没有出现。
以下是一些观察结果:
- 我已经检查过 Java 上是否存在该路径并且确实存在。
- 文件上传成功,因为是returnID,进度为1.0,无消息错误
- 我使用的范围是驱动器全局范围,而不是文件范围。 “https://www.googleapis.com/auth/drive”
- 查询成功出现在 Google 的控制台开发者页面上。
- 我已经尝试使用 root 作为文件夹而不是创建文件夹来创建它。
- 本项目使用的Api版本是"v3-rev90-1.23.0"
这是我的代码的样子(我尝试根据 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:"String of the folder ID"
1.0
文件编号:"String of the file ID"
{}
(其中 {} 应该是驱动器上存在的所有文件)
有人知道为什么它没有出现在驱动器上吗?
您似乎是在使用服务帐户进行身份验证。您需要记住服务帐户不是您,将其视为虚拟用户,它有自己的 Google 驱动器帐户。您一直在将文件上传到服务帐户 Google 驱动器帐户。您可以执行 files.list 来查看这些上传的文件。服务帐户没有 Web 界面视图。
您可以获取服务帐户电子邮件地址并在您的个人 google 驱动器帐户上共享一个目录,这将授予服务帐户上传到该目录的权限。您需要记住让服务帐户修补文件以授予您的用户访问这些文件的权限,否则您将没有任何权限。