Zmiter
Zmiter

Reputation: 328

Google Drive API: The provided file ID is not usable

What rules should I follow to set custom ID for File? I tried short "12345", "abcde", 44-character alphanumeric string, UUID.randomUUID().toString() (with/without dashes) - all attempts return "The provided file ID is not usable". I could not find any documented requirements. The code:

Drive drive = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credentials).build();
FileContent mediaContent = new FileContent("image/png", tempFile);
File body = new File();
body.setId(...);
body.setTitle(...);
body.setMimeType("image/png");
File result = drive.files().insert(body, mediaContent).execute();

The response:

400 Bad Request
{
    "code": 400,
    "errors":
    [{
        "domain": "global",
        "location": "file.id",
        "locationType": "other",
        "message": "The provided file ID is not usable",
        "reason": "invalid"
    }],
    "message": "The provided file ID is not usable"
}

The same code correctly uploads my file to Drive when I am not trying to set ID.

Upvotes: 0

Views: 3271

Answers (2)

kirkham7
kirkham7

Reputation: 71

You can set the id, but you have to use a pre-generated id given to you by Google. I had the same problem, so I dove into the javadoc, and stumbled upon the GeneratedIds Class. Here's the code that works for me:

    int numOfIds = 20;
    GeneratedIds allIds = null;
    try {
        allIds = driveService.files().generateIds()
                .setSpace("drive").setCount(numOfIds).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    List<String> generatedFileIds = allIds.getIds();

Upvotes: 7

seanpj
seanpj

Reputation: 6755

You don't set the the ID, it is assigned by Google Drive at creation. The ID you are talking about is the string you see when you go to drive.google.com, right-click an object (folder/file), and select 'Get link'. You get something like:

https://drive.google.com/open?id=0B1blahblahblahblahfdR25i

and the string '0B1blahblahblahblahfdR25i' is the ID.

Test it. Get it from your drive.google.com, go to the bottom this page - TryIt!, paste the 0B1blahblahblahblahfdR25i into the fileId field.

Back to your problem. You are apparently trying to create a file, try this:

com.google.api.services.drive.Drive mGOOSvc:
...
/**************************************************************************
   * create file in GOODrive
   * @param prnId  parent's ID, (null or "root") for root
   * @param titl  file name
   * @param mime  file mime type
   * @param file  file (with content) to create
   * @return      file id  / null on fail
   */
  static String createFile(String prnId, String titl, String mime, java.io.File file) {
    String rsId = null;
    if (mGOOSvc != null && mConnected && titl != null && mime != null && file != null) try {
      File meta = new File();
      meta.setParents(Arrays.asList(new ParentReference().setId(prnId == null ? "root" : prnId)));
      meta.setTitle(titl);
      meta.setMimeType(mime);

      File gFl = mGOOSvc.files().insert(meta, new FileContent(mime, file)).execute();
      if (gFl != null)
        rsId = gFl.getId();
    } catch (Exception e) { UT.le(e); }
    return rsId;  
  } 

The return, the rsId is the ID you're looking for.

It is taken from an Android demo here (to explain the context), but the Api calls there should be pretty much the same. You can actually pull some CRUD primitives out of there.

Good Luck

Upvotes: 2

Related Questions