【问题标题】:Unable to update file in appDataFolder using Google Drive REST API V3 on Android无法在 Android 上使用 Google Drive REST API V3 更新 appDataFolder 中的文件
【发布时间】:2016-02-28 18:09:19
【问题描述】:

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

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()),我就可以更新元数据。

我该如何解决这个问题?

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

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

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

      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'
      }
      
    9. 再次添加 Jackson 依赖项,以防您现在错过它。如果需要,对其他 google-api-java-client 依赖项执行相同操作。

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 你是对的。显然这是一个错误。 Google 自己的 API 页面链接到不同的问题跟踪器,但 GitHub 上的那个有错误报告:issue #994
      【解决方案3】:

      MediaHttpUploader javadocs 建议它将仅用于 HttpMethods#POST 和 HttpMethods#UPDATE。使用update,基于Files 资源,表明它使用PATCH 方法——导致IllegalArgumentException

      只有在上传媒体内容时才应使用被覆盖的 update 方法。

      【讨论】:

      • 我不确定我是否理解。这正是我试图使用更新方法的目的。文档说更新方法“更新文件的元数据和/或带有补丁的内容语义。”它没有说明文件的资源。它总是补丁。我看不到任何其他更新文件内容的方法。 (除了删除和重新创建文件)。
      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2019-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-07
        • 1970-01-01
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多