Unable to update file in appDataFolder using Googl

2019-02-16 03:16发布

问题:

This is the code i'm using to update the file.

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

This code generates a

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)

Using breakpoints I could see that the String passed to the setInitiationRequestMethod is PATCH (not POST or PUT):

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

this is what i have in my 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'
    }

if I remove the file content (generateFileContents()) I'm able to update the metadata just fine.

How do I solve this?

回答1:

Take a look at the current commit of the google-api-java-client. Unfortunately the fix was not released yet (fix on 21 Nov 2015 vs release on 19 Nov 2015), so you may have to build locally the project (with maven for instance)



回答2:

I ran into this bug while writing a Drive REST API integration for an Android app (with Android Studio/Gradle). Since I'm not particularly experienced with Android's build system, resolving the issue cost me a few hours. Maybe this helps somebody with the same problem:

  1. Clone the google-api-java-client repo from GitHub https://github.com/google/google-api-java-client
  2. Install Maven https://maven.apache.org/run-maven/ (e.g. brew install maven on OSX)
  3. On the command line, change into the google-api-client sub dir of the repo you cloned above
  4. Run mvn clean install
  5. This will produce a subdir called target in the google-api-client directory
  6. In there, find google-api-client-1.22.0-SNAPSHOT.jar, rename it to google-api-client-1.21.00.jar (the renaming is probably not needed)
  7. Drop the .jar in the libs folder of your android project
  8. Tell Gradle to ignore the google-api-client dependency of the libraries you use, in my case this was:

    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. Add the Jackson dependency again, in case you miss it now. Do the same with other google-api-java-client dependencies if you need them.

    compile('com.google.http-client:google-http-client-jackson2:1.21.0'){
        exclude group: 'org.apache.httpcomponents'
    }
    
  10. Build your project, update(...) should now work.

  11. Make a note to scrap these changes once Google has updated the library.


回答3:

The MediaHttpUploader javadocs suggests that it will only be used for HttpMethods#POST, and HttpMethods#UPDATE. Using update, based on the Files resource, indicates its using a PATCH method - leading to the IllegalArgumentException.

The overridden update method should only be used if you're uploading media content.



回答4:

I have the same exception in a Desktop application. Instead, using the Drive Api V2, the update goes well.