Google does not allow such downloads by Google Drive .
To download files through Google Drive via code,
you make a HTTP GET
request for the resource URL
of the file and includes the query parameter alt = media
. Per
example:
GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media
Authorization: Bearer
Downloading the file requires the user to have at least
reading. In addition, your application must be authorized with a scope
that allows reading the contents of the file. For example, a
application that uses the drive.readonly.metadata
scope would not be
authorized to download the contents of the file. Users with permission
can restrict downloading by read-only users
setting the viewersCanCopyContent
field to true
.
Files identified as abusive (malware, etc.) can only be
downloaded by the owner. In addition, the query parameter
acknowledledgeAbuse = true
should be included to indicate that the
threat of downloading potential malware. Your
application must notify the user interactively before using this
query parameter.
I would recommend that you put your file in another site, such as Dropbox , Azure Blob or files.fm , where do not need an exotic code to allow you to download files like Google Drive.
However, if you wanted to continue with Drive , the link below gives you how to do what you want.
Information taken from: Google Drive APIs
EDIT
I ended up passing the wrong information and in the case of Dropbox, you also need to use a DropboxClient
to download files + a Token
generated by the Dropbox API. That said, let's get down to business.
1. Create a Dropbox account
Go to Dropbox and create your account.
2. Create an App to Access a Token
Enter the Dropbox Developers site and create an App that will be used to generate a Token so that we can download of Dropbox files.
Onthescreenthatopens,selectDropboxAPI,thenselectFullDropbox,setanameforyourApp,andthenclickCreateApp.
3.GenerateaToken
Ontheopenscreen,lookforGeneratedaccesstokenandclickGenerate.Oncethisisdone,aTokenwillbegenerated.SavethisTokeninasafeplace.ItwillbeusedtoidentifyyourDropboxaccountviacode.
4.CreateafolderinDropbox
CreateafolderinDropboxandplacethefileyouwanttodownloadinthere.Youcanusethesamefoldertoplaceanytypeoffileyouwanttodownload.
Oncethisisdone,addthedesiredfile(s)intothecreatedfolder.Inthiscase,Iputafile.7z
(zip)withthenameofcv_2018.
5.Createthedownloadcodeforthefile
privatestring_token="token_gerado_nas_etapas_anteriores";
public Form1()
{
InitializeComponent();
btnDownload.Click += Download_Click;
}
/// <summary>
/// Click do botão para download do arquivo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void Download_Click(object sender, EventArgs e)
{
try
{
DropboxClient client;
client = new DropboxClient(_token);
string folder = "DownloadFiles";
string file = "cv_2018.7z";
string destinationFolder = @"C:\Test";
await Download(client, folder, file, destinationFolder);
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao fazer download do arquivo: {ex.Message}");
}
}
/// <summary>
/// Faz o download de um arquivo do Dropbox e salva localmente
/// </summary>
/// <param name="dbx">DropboxClient</param>
/// <param name="folder">Pasta onde se encontra o arquivo no Dropbox</param>
/// <param name="file">Nome do arquivo com a extensão no Dropbox</param>
/// <param name="destinationFolder">Pasta de destino no computador</param>
/// <returns></returns>
private async Task Download(DropboxClient dbx, string folder, string file, string destinationFolder)
{
using (var response = await dbx.Files.DownloadAsync("/" + folder + "/" + file))
{
using (var fileStream = File.Create($"{destinationFolder}\{file}"))
{
(await response.GetContentAsStreamAsync()).CopyTo(fileStream);
}
}
}