0

我正在嘗試使用ASPSnippets.GoogleAP.dllGoogle.Apis.Drive.v3.dll從Google Drive下載文件。但面臨一些挑戰。正在下載文件,但它存在一些奇怪的HTML內容。如何從c#中的谷歌驅動器下載文件?

我使用下面的代碼下載:

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");    
     } 

有人可以幫我解決這個問題?

+0

你可以發佈你收到的錯誤消息嗎?或者你沒有收到錯誤,它只是不下載? – Jay266

+0

@ Jay266我沒有收到任何錯誤msg文件正在下載soem html內容。 –

+0

那麼html說什麼? –

回答

0

我不是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

相關問題