如何使用C#从Google驱动器下载文件并保存到本地文件夹 [英] How to download files from Google drive and save to a folder on local using C#

查看:91
本文介绍了如何使用C#从Google驱动器下载文件并保存到本地文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要从谷歌驱动器下载文件并使用C#保存到本地文件夹。

我按照Google Developer API文档中的描述完成了https://developers.google.com/drive/v3/web/manage-downloads)但我收到的文件格式无效。请建议如何下载。



我尝试过:



downloadUrl =文件的url(例如:https://drive.google.com/uc?id = 1ULNeKdDoCRmWgPPBW8-d1EGUZgqvA1Ul& export = download)

filename =某个带扩展名的页面



 私人  bool  SaveToDir( string  downloadUrl, string  filename){
string filePath = Server.MapPath( imports /);
bool resp = false ;
DriveService ds = new DriveService();
Uri temp = new Uri(downloadUrl);
string fileId = HttpUtility.ParseQueryString(temp.Query).Get( ID);
var req = ds.Files.Get(fileId.Trim());
var stream = new MemoryStream();
req.MediaDownloader.ProgressChanged + =(Google.Apis.Download.IDownloadProgress dp)= > {
开关(dp.Status)
{
案例 Google.Apis.Download.DownloadStatus.Downloading:
消息( 正在下载,请稍候....);
break ;
案例 Google.Apis.Download.DownloadStatus.Completed:
使用(FileStream file = new FileStream(filePath,FileMode.Create,FileAccess.Write))
{
stream.WriteTo(file);
消息( 文件已成功下载。);
}
resp = true ;
break ;
案例 Google.Apis.Download.DownloadStatus.Failed:
消息( 无法下载。);
resp = false ;
break ;
}
};
req.Download(stream);

解决方案

这是因为文件本身已损坏,因为它不是。 doc文件而不是用于登录Google的HTML文档,该文档已作为您要下载的文件下载。



您需要做的是,检查Google是否正在验证您的请求。您没有包含验证用户身份的代码; - 你好吗? OAuth2用户?键?



这完全取决于您对用户进行身份验证的方式,也许从这里开始,关于授权  | 推动REST API  |  Google Developers [ ^ ] 。也是他们页面的一小段摘录,

Quote:

您的应用程序必须使用OAuth 2.0来授权请求​​。不支持其他授权协议。如果您的应用程序使用Google登录,则会为您处理授权的某些方面。

但Google登录如何帮助您,我不确定在此上下文中,因此OAuth2将是一个很好的方法


Hi,
I need to download files from google drive and save to a folder on local using C#.
I have done as described in Google Developer API documentation (https://developers.google.com/drive/v3/web/manage-downloads) but i'm getting the file with invalid format. Please suggest how to download it.

What I have tried:

downloadUrl = url of the file (eg: https://drive.google.com/uc?id=1ULNeKdDoCRmWgPPBW8-d1EGUZgqvA1Ul&export=download)
filename = some name with extension

private bool SaveToDir(string downloadUrl,string filename) {
string filePath = Server.MapPath("imports/");
bool resp = false;
DriveService ds = new DriveService();
Uri temp = new Uri(downloadUrl);
string fileId = HttpUtility.ParseQueryString(temp.Query).Get("id");
var req = ds.Files.Get(fileId.Trim());
var stream = new MemoryStream();
req.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress dp)=> {
switch (dp.Status)
{
 case Google.Apis.Download.DownloadStatus.Downloading:
      Message("downloading, please wait....");
      break;
 case Google.Apis.Download.DownloadStatus.Completed:
      using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                stream.WriteTo(file);
                Message("File Downloaded successfully.");
            }
      resp = true;
      break;
 case Google.Apis.Download.DownloadStatus.Failed:
      Message("Failed to Download.");
      resp = false;
      break;
 }
 };
 req.Download(stream);

解决方案

That is because the file itself is corrupt, as it is not a .doc file rather an HTML document to sign in to Google that has been downloaded as the file you wanted to download.

What you need to do is, check whether Google is authenticating your request or not. You did not include the code to authenticate the users; — how are you doing that? OAuth2? Key?

That entirely depends on how you authenticate the users, perhaps start here, About Authorization  |  Drive REST API  |  Google Developers[^]. Also a small excerpt from their page,

Quote:

Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you.

But how Google Sign-In helps you out, I am unsure of that in this context, thus OAuth2 would be a good way to go.


这篇关于如何使用C#从Google驱动器下载文件并保存到本地文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆