【问题标题】:How to download file from google drive in c#?如何在 C# 中从谷歌驱动器下载文件?
【发布时间】:2017-03-13 17:45:06
【问题描述】:

我正在尝试使用 ASPSnippets.GoogleAP.dllGoogle.Apis.Drive.v3.dll 从 Google 云端硬盘下载文件。但面临一些挑战。正在下载文件,但它包含某种奇怪的 HTML 内容。

我正在使用以下代码下载它:

GoogleConnect.ClientId = "xxxx.apps.googleusercontent.com";
GoogleConnect.ClientSecret = "xxxxxx";
GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
GoogleConnect.API = EnumAPI.Drive;
if (!string.IsNullOrEmpty(Request.QueryString["code"]))
    {
     string code = Request.QueryString["code"];
     string json = GoogleConnect.Fetch("me", code);
     GoogleDriveFiles files = new JavaScriptSerializer().Deserialize<GoogleDriveFiles>(json);

     //Remove the deleted files.
     var driveService = new DriveService();
     Label1.Text = theHiddenField1.Value;
     WebClient wb = new WebClient();
     wb.DownloadFile(theHiddenField1.Value, "C://" + fileName);
    }
    else if (Request.QueryString["error"] == "access_denied")
     { 
       ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);                           
     }
      else
      {   
        GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.readonly");              
       }  

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 您可以发布您收到的错误消息吗?还是您没有收到错误,只是没有下载?
  • @Jay266 我没有收到任何错误消息文件正在下载 soem html 内容。
  • 那么 html 是怎么说的?
  • @AlexK。当我在浏览器页面中打开 html 时,它是 gmail 登录页面。
  • 您可能需要查看此tutorial 关于Google Drive API with C# .net – 下载。此外,在某些情况下,您的主机和特定文件类型之间可能会出现不兼容。单击文件下载链接时,文件实际上并没有下载,而是在您的网络浏览器中出现大量乱码。确保您已正确容纳here 列出的那种格式的文件。

标签: c# google-drive-api google-developers-console google-developer-tools


【解决方案1】:

我不是 C# 开发人员,但如果您使用的是 Google.Apis.Drive.v3,那么这是 Google Drive API 的最新版本。我没有看到任何引用 Google APIs Client Library for .NET 的代码部分,但这是与现代 Google API 对话的推荐方式。

如果您已安装它,请查看C# quickstart sample for the Drive API。然后查看文档中Download Files page 上的 C#/.NET 示例,它应该会引导您找到可行的解决方案。在您的另一条评论中,您询问了有关传递身份验证令牌的问题,因此如果您使用的是客户端库,则不必担心那部分。在该文档页面上,您会找到一个用于常规文件下载的示例和另一个用于导出 Google 文档(文档、表格、幻灯片)的示例。

最后,作为参考,这里是.NET reference docs for the Drive API.NET Google APIs Client Library developers guide

【讨论】:

    猜你喜欢
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2016-10-19
    • 2021-04-05
    相关资源
    最近更新 更多