3

I am trying to create a file in Drive via REST (no SDKs), following the official APIs: https://developers.google.com/drive/v3/reference/files/create

So I follow two steps:

  1. Create file metadata (name, etc)
  2. Upload file content

Step 2 works like a charm, however I face some issues with step 1. Mime type is set fine, but as for the name I always get "Untitled" files, no matter of the actual name or type.

First I tried this:

POST https://www.googleapis.com/upload/drive/v3/files
Authorization: Bearer <OAuth 2.0 access token here>
Content-Type: image/png

{
  "name": "test123.png"
}

Works partially - the file is created, content type is set correctly (e.g. after I write the data in step 2, I can actually preview it in Drive), but the name is "Untitled".

Then I looked again in the docu, and found the following: " Metadata URI, for metadata-only requests: POST https://www.googleapis.com/drive/v3/files " --> this returns 404 (not found).

After this I have tried the API in the API Explorer: https://developers.google.com/drive/v3/reference/files/create#try-it and noticed, that the call is actually against another URI (the file is successfully created and named as expected):

POST https://content.googleapis.com/drive/v3/files
Content-Type: application/json

{
  "name": "test123.png"
}

When I try this in my code, I get 400 Bad Request. Of course I am adding also the bearer token authentication.

I feel I am missing something... can anyone help?

MerlinBG
  • 338
  • 1
  • 5
  • 16
  • Just to be sure, I also tried with "title" attribute, as it is in v2, does not help. – MerlinBG Mar 18 '17 at 18:00
  • If you check Files.create, it requires a uploadType parameter which you did not provide in your POST request body. – ReyAnthonyRenacia Mar 19 '17 at 00:33
  • thanks for the "name": "test123.png" part for v3 since most examples show "title". For me it worked when I changed from "title" to "name" – Vaidas Jun 18 '17 at 19:13

3 Answers3

1

I also faced that problem. I think there is 3 solutions:

  1. Use multipart upload https://developers.google.com/drive/v3/web/multipart-upload with different headers for file metadata and actual file

  2. Upload file in two requests. First to create empty file with metadata (response will provide id of the file) and second to actually "update" the file

  3. Use resumable upload. First request to "setup metadata" (will not even create empty file) and get "special link" where to send request to upload actual file. And this approach have some other features, like uploading in chunks.https://developers.google.com/drive/v3/web/resumable-upload

Here is the link to another Question with implementation of resumable upload in Angular 2

Angular 2+ HTTP POST and GDrive API. Resumable file upload with name

I hope it might be useful.

0

Well if you have a successful meta-data POST from the API explorer, and then your code issues exactly the same request, then of course it must work.

The reason you are getting a file with a title of "Untitled" is that you are POSTing to content endpoint. You are saying "hey Google, create a new file (POST) with this content, but no metadata".

From the snippets you've provided, my guess would be that you are using the wrong Content-Type. For a metadata post (https://content.googleapis.com/drive/v3/files) the content-type is always application/json. The mime-type of the resulting Drive file should be set by the mime-type property in the json.

I suggest take a step back and make sure you understand that, in Drive, the file (as represented by its metadata) is separate to the file contents. Read about "multipart uploads" if you want to provide both in a single call, or better still because it's an image, read about resumable uploads.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • 1
    And once again - thank you :) Yes, the https://content.googleapis.com/drive/v3/files is the right endpoint, between the different attempts I figured out I was supplying invalid JSON... *blushing*. Any idea why https://developers.google.com/drive/v3/reference/files/create is not mentioning about the *contents* endpoint? About multipart and resumable uploads, I checked them, but the guide suggests for smaller files to go with single upload - even my images are max 200k each - so I went for that, is it right? – MerlinBG Mar 18 '17 at 23:30
  • Although posting to the content endpoint works, it rarely makes sense to create a file without a title, so isn't widely documented to save confusion. I agree that resumable upload isn't necessary for <256kb unless it's a particularly unreliable or slow connection. – pinoyyid Mar 19 '17 at 22:19
0

In my case Untitled files in Google Drive created only for release apk. Fix by updating proguard-rules. I added rules for Google Drive REST API:

-keep class com.google.** { *;}
-keep class com.fasterxml.** { *;}
user1795683
  • 196
  • 3
  • 5