将驱动器 API 与 Java 一起使用

Using Drive API with Java

我正在按照 Google 文档将文件上传到 Google Drive on windows with Java,但是我在以下行中有一个错误我不能弄清楚为什么我有它! https://developers.google.com/drive/v2/reference/files/insert

File file = service.files().insert(body, mediaContent).execute();

我有所有需要的 .jar 文件,错误是:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method insert(File, FileContent) is undefined for the type Drive.Files

当我将光标移动到靠近插入的位置时,它显示添加演员表到 service.files()

有人可以帮我吗? 我的代码与文档完全一样(如果向下滚动 link 我提供的请)

感谢您的帮助。

您正在使用 v2 Google 驱动器 API 代码,但您包含了 v3 JAR 文件。如果你真的想使用 v3 那么你将不得不使用 v3 API。意识到 insert() 显然现在称为 create()。所以你应该用这些行替换你的代码行:

File file = service.files().create(body, mediaContent).execute();

如果您希望使用 v2 API,则必须切换到 v2 JAR。 如果您选择使用 v2 依赖项,您的 POM 可能如下所示:

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-drive</artifactId>
    <version>v2-rev206-1.21.0</version>
</dependency>