2

I am using the Google Drive API v3 to retrieve a file. I can upload files and list files, but the files.get call returns 'false'. The documentation is pretty sparse (https://developers.google.com/drive/v3/reference/files/get)

var contentRequest = gapi.client.drive.files.get({
    fileId: fileId,
    alt: 'media'
  });
  contentRequest.execute(function(resp) {
    console.log(resp); // resp = false
  });

If I remove the "alt: 'media'" parameter, then I get back the metadata as described in the documentation. I've also tried using:

gapi.client.request
    ( { 
    'path': '/drive/v3/files/'+fileId,
    'method': 'GET',
    'params': {'fileId': fileId, 'alt': 'media'},
    'headers': {'Authorization': 'Bearer ' + gapi.auth.getToken().access_token }
    }).execute(function(file) { 
         // file = false 
      }); 

The only breadcrumb of hope that I can find is buried away in the Google Client REST API documentation, which says that the response is 'false' if not JSON-parseable. https://developers.google.com/api-client-library/javascript/features/promises

The type of file appears to make no difference... So far I've tried .html and .txt.

Kerri
  • 45
  • 7

3 Answers3

2

When calling execute on the request object I get a false response. But when calling "then" it works!

request.then(function(resp) {
   console.log(resp);
   console.log(resp.body); // The file content
});
user5886944
  • 174
  • 1
  • 7
1

You need to use the second parameter of the callback - it should contain the raw response.

request.execute(function(jsonResp, rawResp) {
    console.log('rawResp: ', rawResp);
    var respObject = JSON.parse(rawResp); // rawResp is encoded JSON string with header, body, etc.
    var respBody = respObject.gapiRequest.data.body; // in my case here it outputs the text of my txt file
});

The callback function which executes when the request succeeds or fails. jsonResp contains the response parsed as JSON. If the response is not JSON, this field will be false. rawResp is the HTTP response. It is JSON, and can be parsed to an object which includes body, headers, status, and statusText fields.

src: https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequest

P.S. Nice find with docs about "not JSON-parseable"! It helped me to solve this!

DarthVanger
  • 1,063
  • 10
  • 10
  • rawResp is [{"id":"gapiRpc","result":false}] ... any more ideas ? – user5886944 Jun 14 '18 at 13:00
  • @user5886944 no `body`, no `status`, no nothing more in that `rawResp`? Quite strange... Try getting another file of another format, or even making request for something else. – DarthVanger Jun 14 '18 at 19:46
0

I believe this is the link you are looking for : )

https://developers.google.com/drive/v3/web/manage-downloads

Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.

Chris Chen
  • 1,228
  • 9
  • 14
  • This does not answer the question. I am having same issue and already had read ( and followed ) the referenced link. I am getting false as a response desipite having full access to the drive and using {fileId:id, alt:'media'}) in the request. – Eric G Feb 18 '17 at 03:44