How to download Drive files

101 views
Skip to first unread message

Siva Thiyagu

unread,
Jan 24, 2018, 2:26:01 AM1/24/18
to Google APIs Client Library for C++
We are able to create directories, upload files and list directory contents using this  Google APIs Client Library in C++.

How do we download a file from the drive? What should be done after executing the FilesResource_GetMethod ? 

    1) Is the file already downloaded and available as JsonData after executing FilesResource_GetMethod ? If so should we connect it to a DataWriter to write into a file ?

    2) OR if we need to further query the "download url" of the file then how is that done?

Any assistance is greatly appreciated in this regard.

Here is the code we are using that lists all files from a directory. 

--

  std::unique_ptr<google_drive_api::File> filePtr(google_drive_api::File::New());
  const DriveService* _service_ = service_.get();
  ChildrenResource_ListMethod listFiles( _service_, &credential_, <file_id_of_dir>);

  ChildList* dataList = ChildList::New();
  status = listFiles.ExecuteAndParseResponse(dataList);
  
  const client::JsonCppArray<ChildReference> chRef1 = dataList->get_items();

  for( int ii=0; ii<chRef1.size(); ii++ ) {
    const StringPiece &fileId = chRef1[ii].get_id();

    FilesResource_GetMethod *fileGetPtr = 
      _service_->get_files().NewGetMethod( &credential_, fileId);

    google_drive_api::File *filePtr = google_drive_api::File::New();
    googleapis::util::Status status = fileGetPtr->ExecuteAndParseResponse(filePtr);

    if(status.ok())
      std::cout<<filePtr->get_title()<<" : ("<<fileId<<")"<<std::endl;
   
  }
  string filePath("/home/admin/temp/");
  googleapis::client::DataWriter *dataPtr = googleapis::client::NewFileDataWriter(filePath);

--

-Siva

Tony Aiuto

unread,
Jan 24, 2018, 9:31:30 AM1/24/18
to Siva Thiyagu, Google APIs Client Library for C++
On Wed, Jan 24, 2018 at 2:26 AM, Siva Thiyagu <si...@cuedio.com> wrote:
We are able to create directories, upload files and list directory contents using this  Google APIs Client Library in C++.

How do we download a file from the drive? What should be done after executing the FilesResource_GetMethod ? 

    1) Is the file already downloaded and available as JsonData after executing FilesResource_GetMethod ? If so should we connect it to a DataWriter to write into a file ?

    2) OR if we need to further query the "download url" of the file then how is that done?

​Yes. You must use downloadUrl to get the file. Something like
 

Any assistance is greatly appreciated in this regard.

Here is the code we are using that lists all files from a directory. 

--

  std::unique_ptr<google_drive_api::File> filePtr(google_drive_api::File::New());
  const DriveService* _service_ = service_.get();
  ChildrenResource_ListMethod listFiles( _service_, &credential_, <file_id_of_dir>);

  ChildList* dataList = ChildList::New();
  status = listFiles.ExecuteAndParseResponse(dataList);
  
  const client::JsonCppArray<ChildReference> chRef1 = dataList->get_items();

  for( int ii=0; ii<chRef1.size(); ii++ ) {
    const StringPiece &fileId = chRef1[ii].get_id();

    FilesResource_GetMethod *fileGetPtr = 
      _service_->get_files().NewGetMethod( &credential_, fileId);

    google_drive_api::File *filePtr = google_drive_api::File::New();
    googleapis::util::Status status = fileGetPtr->ExecuteAndParseResponse(filePtr);

    if(status.ok())
      std::cout<<filePtr->get_title()<<" : ("<<fileId<<")"<<std::endl;
   

​       get downloadUrl from chRef1[i]

   ​    std::unique_ptr<HttpRequest> request(
            app_->service()->transport()->NewHttpRequest(HttpRequest::GET));
      request->set_url(url);
      // you have to pass the same credentials from the app.
      request->set_credential(app_->credential());
 
  
  string filePath("/home/admin/temp/");
  googleapis::client::DataWriter *dataPtr = googleapis::client::NewFileDataWriter(filePath);
​ 
     request->set_content_writer(dataPtr);​
​     request->Execute().IgnoreError();
     HttpResponse* download_response = request->response();

​If you don't set a content writer, you'll get the whole file download_response BodyString.
But that would be terrible for big files.
 

--

-Siva

Siva Thiyagu

unread,
Jan 24, 2018, 10:51:30 AM1/24/18
to Google APIs Client Library for C++
@Tony - Thanks for the pointers.

How exactly we get the " get downloadUrl from ChildReference is the key I guess. I have grep-ed for "url/download/link" and a few through out the client api code and havnt been able to locate any information on the "download url" attribute.

If anyone knows please respond with code snippets.

Siva Thiyagu

unread,
Feb 13, 2018, 8:32:15 AM2/13/18
to Google APIs Client Library for C++
We got the download url - but the http get request is failing... 

====
      std::cout<<filePtr->get_title()<<" : ("<<fileId<<")";//<<std::endl;
      googleapis::StringPiece strpc = filePtr->get_download_url();
      std::cout<<" => ("<<strpc<<")"<<std::endl;
      googleapis::client::HttpTransport* ptrHttp = service_->transport();
      string filePath("/home/admin/temp/test.txt");
      googleapis::client::DataWriter *dataPtr = googleapis::client::NewFileDataWriter(filePath);
      googleapis::client::HttpRequest *ptrHttpReq =
        ptrHttp->NewHttpRequest(googleapis::client::HttpRequest::GET);
      ptrHttpReq->set_url(strpc.as_string());
      ptrHttpReq->set_content_writer(dataPtr);
      googleapis::util::Status status = ptrHttpReq->Execute();
      if(status.ok())
        std::cout<<" HttpRequest PASS"<<std::endl;
      else
        std::cout<<" HttpRequest FAILED"<<std::endl;

    }
====

Would anyone know why  ptrHttpReq->Execute() fails? since this is a real-time usecase, how does one debug the failure reasons?

Siva Thiyagu

unread,
Feb 14, 2018, 3:26:34 AM2/14/18
to Google APIs Client Library for C++
The failure reason was "unauthorized access" adding    ptrHttpReq->set_credential(&credential_); solved it.
Reply all
Reply to author
Forward
0 new messages