簡體   English   中英

如何在C#中從Google驅動器下載文件?

[英]How to download file from google drive in c#?

我正在嘗試使用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");              
       }  

有人可以幫我解決問題嗎?

我不是C#開發人員,但是如果您使用的是Google.Apis.Drive.v3 ,那么這是Google Drive API的最新版本。 我看不到您的代碼中引用了.NETGoogle API客戶端庫的任何部分,但這是與現代Google API進行通信的推薦方式。

如果已安裝,請查看Drive APIC#快速入門示例 然后在文檔的“ 下載文件”頁面上查看C#/。NET示例,它應可帶您找到可行的解決方案。 在您的其他評論中,您詢問是否傳遞身份驗證令牌,因此,如果您使用的是客戶端庫,則不必擔心該部分。 在該文檔頁面上,您將找到一個用於常規文件下載的示例,另一個用於導出Google文檔(文檔,表格,幻燈片)的示例。

最后,作為其他參考,這是Drive API.NET參考文檔.NET Google API客戶端庫開發人員指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM