Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to retrieve file content from google drive API

I seem to be unable to retrieve the content of a file via the google drive SDK. To reproduce the problem I use the API explorer to get the meta data for a small text file:

200 OK

- Show headers -

{

 "kind": "drive#file",
 "id": "0B75zvzRT_NusaDBtSTVmWWk1cVk",
 "etag": "\"B6kWtzuiQYCrVi2MWyRaub0CRyo/MTM1NTgzNDEzMjU5MA\"",
 "selfLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusaDBtSTVmWWk1cVk",
 "webContentLink": "https://docs.google.com/uc?id=0B75zvzRT_NusaDBtSTVmWWk1cVk&export=download",
 "alternateLink": "https://docs.google.com/file/d/0B75zvzRT_NusaDBtSTVmWWk1cVk/edit",
 "iconLink": "https://ssl.gstatic.com/docs/doclist/images/icon_10_text_list.png",
 "thumbnailLink":     "https://lh4.googleusercontent.com/ntOMGJ9Is5KigDUzpOe6xKRqVqHPyeeXPImG1vZTF2FDM5YOyeCZT62FsdBWTnetBA=s220",
 "title": "sync.txt",
 "mimeType": "text/plain",
 "description": "description",
 "labels": {
  "starred": false,
  "hidden": false,
  "trashed": false,
  "restricted": false,
  "viewed": true
 },
 "createdDate": "2012-12-18T11:49:21.710Z",
 "modifiedDate": "2012-12-18T12:35:32.590Z",
 "modifiedByMeDate": "2012-12-18T12:35:32.590Z",
 "lastViewedByMeDate": "2012-12-18T12:35:32.416Z",
 "parents": [
  {

   "kind": "drive#parentReference",
   "id": "0B75zvzRT_NusdVVJeGk2dVc2VHM",
   "selfLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusaDBtSTVmWWk1cVk/parents/0B75zvzRT_NusdVVJeGk2dVc2VHM",
   "parentLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusdVVJeGk2dVc2VHM",
   "isRoot": false
  }
 ],
 "downloadUrl": "https://doc-0g-0-docs.googleusercontent.com/docs/securesc/b2hod7vud4bdud0ju4mut5hh2assmdju/rmi0iqd62g0im724ngmc5uva7femfffo/1355832000000/00903399969355284739/00903399969355284739/0B75zvzRT_NusaDBtSTVmWWk1cVk?h=16653014193614665626&e=download&gd=true",
 "userPermission": {
  "kind": "drive#permission",
  "etag": "\"B6kWtzuiQYCrVi2MWyRaub0CRyo/kH0lkP-s4aFu1o5itR2fFqyLM6o\"",
  "id": "me",
  "selfLink": "https://www.googleapis.com/drive/v2/files/0B75zvzRT_NusaDBtSTVmWWk1cVk/permissions/me",
  "role": "owner",
  "type": "user"
 },
 "originalFilename": "sync.txt",
 "fileExtension": "txt",
 "md5Checksum": "ecd21579645508d1c206d5e6e20fd101",
 "fileSize": "156",
 "quotaBytesUsed": "156",
 "ownerNames": [
  "Sam Smith"
 ],
 "lastModifyingUserName": "Sam Smith",
 "editable": true,
 "writersCanShare": true
}

I then click on the downloadUrl link and I always get an empty response i.e a 200 response with an empty body. If I remove the "gd=true" parameter from the end of the Url it downloads fine. In my program the same thing happens except removing the "gd=true" also does not work (probably because the program runs on a server and therefore I am not logged into my account).

My main question is why does the downloadUrl link not return a response without fiddling with the parameters?

UPDATE: Found other people reporting the exact same issue Here, Here and Here

SOLUTION: OK I finally worked it out. It seems for the GET request on the downloadURL you need to send the access token in the headers i.e send a "Authorization: Bearer {your access token}" header. DO NOT send the access token as part of the query string. Every other API call I have used in the Drive SDK is fine with the access token as part of the query string - except this one.

like image 926
blissland Avatar asked Dec 18 '12 13:12

blissland


People also ask

Why can't I access my Google Drive files?

If a file won't open, a few things could be wrong: The file owner didn't give you permission to view the file. You're signed in to a different Google Account. Your access could be denied because someone removed your permission to view the file.

How do I fix the 403 user Rate limit exceeded?

Resolve a 403 error: Project rate limit exceededRaise the per-user quota in the Google Cloud project. For more information, request a quota increase. Batch requests to make fewer API calls. Use exponential backoff to retry the request.


1 Answers

OK I finally worked it out. It seems for the GET request on the downloadURL you need to send the access token in the headers i.e send a "Authorization: Bearer {your access token}" header. DO NOT send the access token as part of the query string. Every other API call I have used in the Drive SDK is fine with the access token as part of the query string - except this one.

like image 200
blissland Avatar answered Oct 24 '22 01:10

blissland