public static bool download_file_from_googledirve(string file_name, int contribution_id)
{
try
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = configurations.googledrive_client_id;
provider.ClientSecret = configurations.googledrive_client_secret;
IAuthenticator authenticator = new OAuth2Authenticator<NativeApplicationClient>(provider, googledrive_getauthorization);
DriveService gd_service = new DriveService(authenticator);
FilesResource.ListRequest list_request = gd_service.Files.List();
list_request.Q = "title = '" + file_name + "'";
FileList file_list = list_request.Fetch();
byte[] transfer_buffer = new byte[configurations.download_buffer_size];
int downloaded_files_count = 0;
foreach (Google.Apis.Drive.v2.Data.File f in file_list.Items)
{
if (String.IsNullOrEmpty(f.DownloadUrl)) continue;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(f.DownloadUrl));
authenticator.ApplyAuthenticationToRequest(request);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK) continue;
System.IO.FileStream file_stream = new System.IO.FileStream(
configurations.GetAbsoluteContributionPath() + contribution_id.ToString() + "." + f.FileExtension, System.IO.FileMode.CreateNew);
System.IO.Stream response_stream = response.GetResponseStream();
int bytes_read = 0;
while ((bytes_read = response_stream.Read(transfer_buffer, 0, transfer_buffer.Length)) > 0) { file_stream.Write(transfer_buffer, 0, bytes_read); }
downloaded_files_count++;
file_stream.Close();
response_stream.Close();
}
if (downloaded_files_count > 0) return true;
else return false;
}
catch (Exception gd_exc)
{
// write log of the exception
log.WriteErrorLog(gd_exc);
return false;
}
}