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

相关问题