无法在 Android 上使用 Google Drive REST API V3 更新 appDataFolder 中的文件

Unable to update file in appDataFolder using Google Drive REST API V3 on Android

这是我用来更新文件的代码。

File metadata = generateFileMetadata(fileId, thumbnail, properties);
return mService.files().update(fileId, metadata, generateFileContents())
                    .setFields("id, name, appProperties")
                    .execute();

此代码生成

java.lang.IllegalArgumentException.
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.media.MediaHttpUploader.setInitiationRequestMethod(MediaHttpUploader.java:872)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.initializeMediaUpload(AbstractGoogleClientRequest.java:237)
at com.google.api.services.drive.Drive$Files$Update.<init>(Drive.java:3163)
at com.google.api.services.drive.Drive$Files.update(Drive.java:3113)

使用断点我可以看到传递给 setInitiationRequestMethod 的字符串是 PATCH(不是 POSTPUT):

public MediaHttpUploader setInitiationRequestMethod(String initiationRequestMethod) {
    Preconditions.checkArgument(initiationRequestMethod.equals(HttpMethods.POST)
    || initiationRequestMethod.equals(HttpMethods.PUT));
    this.initiationRequestMethod = initiationRequestMethod;
    return this;
  }

这就是我的 build.gradle

compile 'com.google.android.gms:play-services-identity:8.4.0'
    compile('com.google.api-client:google-api-client-android:1.21.0') {
    exclude group: 'org.apache.httpcomponents'
    }
    compile('com.google.apis:google-api-services-drive:v3-rev11-1.21.0') {
    exclude group: 'org.apache.httpcomponents'
    }

如果我删除文件内容 (generateFileContents()),我就能很好地更新元数据。

我该如何解决这个问题?

MediaHttpUploader javadocs suggests that it will only be used for HttpMethods#POST, and HttpMethods#UPDATE. Using update, based on the Files 资源,表示它使用 PATCH 方法 - 导致 IllegalArgumentException

重写的 update 方法只应在您上传媒体内容时使用。

我在桌面应用程序中遇到了同样的异常。 相反,使用 Drive Api V2,更新很顺利。

看看 google-api-java-客户端的 current commit。 不幸的是,修复尚未发布(2015 年 11 月 21 日修复 vs 2015 年 11 月 19 日发布),因此您可能必须在本地构建项目(例如使用 maven)

我 运行 在为 Android 应用程序(使用 Android Studio/Gradle)编写 Drive REST API 集成时遇到了这个错误。由于我对 Android 的构建系统没有特别的经验,解决这个问题花了我几个小时。也许这可以帮助遇到同样问题的人:

  1. 从 GitHub https://github.com/google/google-api-java-client
  2. 克隆 google-api-java-client 存储库
  3. 安装 Maven https://maven.apache.org/run-maven/(例如 OSX 上的 brew install maven
  4. 在命令行中,更改为您在上面克隆的存储库的 google-api-client 子目录
  5. 运行 mvn clean install
  6. 这将在 google-api-client 目录中生成一个名为 target 的子目录
  7. 在那里,找到google-api-client-1.22.0-SNAPSHOT.jar,将其重命名为google-api-client-1.21.00.jar(可能不需要重命名)
  8. 将 .jar 拖放到 android 项目的 libs 文件夹中
  9. 告诉 Gradle 忽略您使用的库的 google-api-客户端依赖性,在我的例子中是:

    compile('com.google.api-client:google-api-client-android:1.21.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude module: 'google-api-client'
    }
    compile('com.google.apis:google-api-services-drive:v3-rev14-1.21.0') {
        exclude group: 'org.apache.httpcomponents'
        exclude module: 'google-api-client'
    }
    
  10. 再次添加 Jackson 依赖项,以防您现在错过它。如果需要,对其他 google-api-java-客户端依赖项执行相同的操作。

    compile('com.google.http-client:google-http-client-jackson2:1.21.0'){
        exclude group: 'org.apache.httpcomponents'
    }
    
  11. 构建您的项目,update(...) 现在应该可以工作了。

  12. 一旦 Google 更新了库,请记下废弃这些更改。