Downloads the content of a file by ID. (In this example the file id = 1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl) The file is streamed directly to the filesystem. Note: The alt=media query param must be used to download the file content (as opposed to the file metadata).
#include <CkRest.h>
#include <CkOAuth2.h>
#include <CkStringBuilder.h>
#include <CkStream.h>
void ChilkatSample(void)
{
CkRest rest;
bool success;
// Provide a previously obtained OAuth2 access token.
CkOAuth2 oauth2;
oauth2.put_AccessToken("OAUTH2_ACCESS_TOKEN");
rest.SetAuthOAuth2(oauth2);
success = rest.Connect("www.googleapis.com",443,true,true);
if (success != true) {
std::cout << rest.lastErrorText() << "\r\n";
return;
}
rest.AddQueryParam("includeTeamDriveItems","true");
rest.AddQueryParam("supportsTeamDrives","true");
rest.AddQueryParam("alt","media");
// First send the HTTP request.
success = rest.SendReqNoBody("GET","/drive/v3/files/1R_70heIyzIAu1_u0prXbYcaIiJRVkgBl");
if (success != true) {
std::cout << rest.lastErrorText() << "\r\n";
return;
}
// Read the response header. If the response status code is success, stream to the file.
// Otherwise receive the error response text.
int statusCode = rest.ReadResponseHeader();
if (statusCode < 0) {
std::cout << rest.lastErrorText() << "\r\n";
return;
}
if (statusCode != 200) {
std::cout << "Received error response code: " << statusCode << "\r\n";
// Read the error response body.
CkStringBuilder sbErrResponse;
success = rest.ReadRespSb(sbErrResponse);
if (success != true) {
std::cout << rest.lastErrorText() << "\r\n";
return;
}
std::cout << "Error response:" << sbErrResponse.getAsString() << "\r\n";
return;
}
// Stream the response body to the output file.
CkStream respBodyStream;
respBodyStream.put_SinkFile("/someDirectory/penguins.jpg");
success = rest.ReadRespBodyStream(respBodyStream,true);
if (success != true) {
std::cout << rest.lastErrorText() << "\r\n";
return;
}
std::cout << "Example Completed." << "\r\n";
}