How can I implement a resumable upload feature in Google Drive SDK?

I’m having trouble setting up a resumable upload in the Google Drive SDK. I’ve checked the docs but I’m stuck on getting the ‘Location’ header from the initial request. When I try a POST request, it creates an empty file without the Location header. A PUT request just gives me a 404 error. Here’s what I’ve tried:

POST /upload/drive/v2/files?uploadType=resumable
Headers:
  Accept: application/json
  Authorization: Bearer myToken123
  Content-Type: application/json
  X-Upload-Content-Length: 1000
  X-Upload-Content-Type: text/plain

Body:
{
  'mimeType': 'text/plain',
  'parents': [{'id': 'root'}],
  'title': 'myFile.txt'
}

The response creates a file but doesn’t include the Location header I need. Any ideas on what I’m doing wrong or how to get this working?

I’ve dealt with similar issues implementing resumable uploads with Google Drive SDK. Here’s what worked for me:

Make sure you’re using the correct endpoint. For v3 of the API, it should be:

https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

Also, double-check your authorization. The token might be expired or have insufficient permissions.

One trick that helped me was to explicitly set the ‘Content-Length’ header to 0 for the initial request. This tells Google you’re only creating the upload session, not sending data yet.

If you’re still not getting the Location header, try enabling verbose logging in your HTTP client. Sometimes the header is there, but not easily visible.

Lastly, ensure you’re handling the response correctly. The Location header should be in the response to your initial POST request, not in subsequent PUT requests.

Hope this helps! Let me know if you need more details.

hey, i had this problem too. make sure ur using the right endpoint for v3 api. also, set content-length to 0 in the initial POST. that fixed it for me. check ur oauth scope too - should have drive.file or broader. if u still cant see the location header, try printing all response headers. good luck!

I encountered a similar issue when implementing resumable uploads. One key aspect that helped me was ensuring the correct API version and endpoint. For v3, use ‘https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable’.

Another crucial point is handling the initial request properly. Make sure you’re sending a POST request with an empty body, and set the ‘Content-Length’ header to 0. This signals to Google that you’re initiating an upload session.

If you’re still not receiving the Location header, try inspecting the full response, including all headers. Sometimes it’s present but not immediately obvious.

Lastly, verify your OAuth scope includes https://www.googleapis.com/auth/drive.file or a broader scope that encompasses file creation and uploads. Incorrect scopes can lead to unexpected behavior.