Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#从Google Drive导出/下载文件会生成一个空的零字节文件_C#_Api_Download_Export_Cloud - Fatal编程技术网

使用C#从Google Drive导出/下载文件会生成一个空的零字节文件

使用C#从Google Drive导出/下载文件会生成一个空的零字节文件,c#,api,download,export,cloud,C#,Api,Download,Export,Cloud,我试图使用C#&Google.api.Drive.v3从谷歌硬盘下载一个文件,得到一个空的零字节文件(见下面的代码)。我正在上传文件,但无法下载。任何帮助都将不胜感激 code static string[] Scopes = { DriveService.Scope.Drive }; static string ApplicationName = "Test001"; private DriveService _service = null; public async Task downloa

我试图使用C#&Google.api.Drive.v3从谷歌硬盘下载一个文件,得到一个空的零字节文件(见下面的代码)。我正在上传文件,但无法下载。任何帮助都将不胜感激

code

static string[] Scopes = { DriveService.Scope.Drive };
static string ApplicationName = "Test001";
private DriveService _service = null;
public async Task downloadFile(string url, string lstrDownloadFile)
{

    // Authorize API access
    UserCredential credential;

    using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
    {
        // The file token.json stores the user's access and refresh tokens, and is created
        // automatically when the authorization flow completes for the first time.
        string credPath = "token.json";
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
        Debug.WriteLine("Credential file saved to: " + credPath);
    }

    // Create Drive API service.
    _service = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    // Attempt download
    // Iterate through file-list and find the relevant file
    FilesResource.ListRequest listRequest = _service.Files.List();
    listRequest.Fields = "nextPageToken, files(id, name, mimeType, originalFilename, size)";
    Google.Apis.Drive.v3.Data.File lobjGoogleFile = null;
    foreach (var item in listRequest.Execute().Files)
    {
        if (url.IndexOf(string.Format("id={0}", item.Id)) > -1)
        {
            Debug.WriteLine(string.Format("{0}: {1}", item.OriginalFilename, item.MimeType));
            lobjGoogleFile = item;
            break;
        }
    }

    FilesResource.ExportRequest request = _service.Files.Export(lobjGoogleFile.Id, lobjGoogleFile.MimeType);
    Debug.WriteLine(request.MimeType);
    MemoryStream lobjMS = new MemoryStream();
    await request.DownloadAsync(lobjMS);

    // At this point the MemoryStream has a length of zero?

    lobjMS.Position = 0;
    var lobjFS = new System.IO.FileStream(lstrDownloadFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
    await lobjMS.CopyToAsync(lobjFS);
}

可能是因为没有在项目中启用驱动器API这样简单

我建议您添加以下代码。可能存在下载错误,导致流无法填充

FilesResource.ExportRequest request = ...
request.MediaDownloader.ProgressChanged += progress =>
{
    switch (progress.Status)
    {
        case DownloadStatus.Failed:
        {
            Console.WriteLine("Failed: " + progress.Exception?.Message);
            break;
        }
        // other status case statements if you need them
    }
};

MemoryStream lobjMS = ...

然后,您可以在失败的案例中放置断点或在控制台中查看异常。

应该添加lobjGoogleFile.Size=34596,因此我尝试下载的不是零字节文件。