有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Google Drive API时,不会显示java上载的文件

我在使用Google Drive API上传文件时遇到一些问题。API不会返回任何错误,并显示文件已成功上载。但是,当我尝试检索文件列表时,它不会出现,当我尝试查看Google Drive API页面时,它也不会出现

以下是一些观察结果:

  • 我已经检查了Java上是否存在该路径,它确实存在
  • 文件上载成功,因为它返回一个ID,进度为1.0,并且没有消息错误
  • 我使用的范围是驱动器全局范围,而不是文件范围。“https://www.googleapis.com/auth/drive
  • 查询成功出现在Google的console developers页面上
  • 我已经尝试使用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:“文件夹ID的字符串”
1.0
文件ID:“文件ID的字符串”
{}
(其中{}应该是驱动器上的所有文件)

有人知道为什么它没有出现在硬盘上吗


共 (1) 个答案

  1. # 1 楼答案

    您似乎正在使用服务帐户进行身份验证。你需要记住,一个服务帐户不是你,把它想象成一个虚拟用户——它有自己的谷歌驱动器帐户。您已将文件上载到服务帐户Google drive帐户。你可以做一个文件。列出以查看这些上传的文件。服务帐户没有web界面视图

    您可以获取服务帐户的电子邮件地址,并在您的个人google drive帐户上共享一个目录。这将授予服务帐户上载到此目录的权限。您需要记住让服务帐户修补文件,以授予用户对文件的访问权限,否则您将无权访问这些文件