C# 从谷歌硬盘下载电子表格

C# 从谷歌硬盘下载电子表格,c#,google-drive-api,google-api-dotnet-client,C#,Google Drive Api,Google Api Dotnet Client,我正在尝试使用FileResourceAPI中的ExportLinks从Google Drive下载一个工作表 但是,我得到的响应不是电子表格,而是表示电子表格或类似内容的HTML文件 以下是我正在使用的请求: FilesResource.GetRequest getF = new FilesResource.GetRequest(service, "1y92Rok6oYKMwvc-Oq4Uurah7y552sfmIbyD9Wzmpq54"); G

我正在尝试使用FileResourceAPI中的ExportLinks从Google Drive下载一个工作表

但是,我得到的响应不是电子表格,而是表示电子表格或类似内容的HTML文件

以下是我正在使用的请求:

            FilesResource.GetRequest getF = new FilesResource.GetRequest(service, "1y92Rok6oYKMwvc-Oq4Uurah7y552sfmIbyD9Wzmpq54");
            Google.Apis.Drive.v2.Data.File f = getF.Execute();
            string downloadUrl = f.ExportLinks["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"];
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(downloadUrl));
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            ReadWriteStream(response.GetResponseStream(), File.OpenWrite("D:\\tmp.xlsx"));
我用于存储流的函数:

static private void ReadWriteStream(Stream readStream, Stream writeStream)
        {
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            // write the required bytes
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();
        }

有人能指出我做错了什么吗?

这是我通常使用的方法

/// <summary>
        /// Download a file
        /// Documentation: https://developers.google.com/drive/v2/reference/files/get
        /// </summary>
        /// <param name="_service">a Valid authenticated DriveService</param>
        /// <param name="_fileResource">File resource of the file to download</param>
        /// <param name="_saveTo">location of where to save the file including the file name to save it as.</param>
        /// <returns></returns>
        public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
        {

            if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
            {
                try
                {
                    var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
                    byte[] arrBytes = x.Result;
                    System.IO.File.WriteAllBytes(_saveTo, arrBytes);
                    return true;                  
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return false;
                }
            }
            else
            {
                // The file doesn't have any content stored on Drive.
                return false;
            }
        }
//
///下载文件
///文件:https://developers.google.com/drive/v2/reference/files/get
/// 
///有效的经过身份验证的驱动器服务
///要下载的文件的文件资源
///保存文件的位置,包括保存文件的文件名。
/// 
公共静态布尔下载文件(DriveService\u服务、文件\u文件资源、字符串\u保存到)
{
如果(!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
尝试
{
var x=_service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl);
字节[]arrBytes=x.结果;
System.IO.File.writealBytes(_saveTo,arrBytes);
返回true;
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
返回false;
}
}
其他的
{
//该文件在驱动器上未存储任何内容。
返回false;
}
}