Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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# 使用.NET下载Google文档时遇到问题?_C#_.net_Google Drive Api_Google Docs Api - Fatal编程技术网

C# 使用.NET下载Google文档时遇到问题?

C# 使用.NET下载Google文档时遇到问题?,c#,.net,google-drive-api,google-docs-api,C#,.net,Google Drive Api,Google Docs Api,我已经为access google drive API创建了示例web应用程序 使用我的google帐户启用google drive文档API 已下载credential.json文件并包含在项目中 用下面的代码将一个文档上传到我的谷歌硬盘 下载文件流 但文件流在随机情况下不能正确下载 参考链接: 如何实现这一点?但文件流在随机情况下无法正确下载。这意味着什么?流程完成后,您是否检查了流的内容?是的,我将流保存为文件。该文件没有反映我的更改System.IO.file.writealBytes(

我已经为access google drive API创建了示例web应用程序

  • 使用我的google帐户启用google drive文档API
  • 已下载credential.json文件并包含在项目中
  • 用下面的代码将一个文档上传到我的谷歌硬盘
  • 下载文件流
  • 但文件流在随机情况下不能正确下载

    参考链接:


    如何实现这一点?

    但文件流在随机情况下无法正确下载。
    这意味着什么?流程完成后,您是否检查了流的内容?是的,我将流保存为文件。该文件没有反映我的更改
    System.IO.file.writealBytes(SavedLocation,stream.ToArray())UserCredential credential;
    string[] scopes = new string[] { DriveService.Scope.Drive,
                        DriveService.Scope.DriveAppdata,
                        DriveService.Scope.DriveFile,
                        DriveService.Scope.DriveMetadataReadonly,
                        DriveService.Scope.DriveReadonly,
                        DriveService.Scope.DriveScripts
    };
    
    using (var stream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "credentials.json", FileMode.Open, FileAccess.ReadWrite))
    {
      string credPath = AppDomain.CurrentDomain.BaseDirectory + "token.json";
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    scopes,
    "user",
    CancellationToken.None, 
    new FileDataStore(credPath, true)).Result;
     }
    
    DriveService driveService = new DriveService(new BaseClientService.Initializer()
    {
     HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
     });
    
    FilesResource.CreateMediaUpload request;
    using (var stream = new System.IO.FileStream(fileName,                                            System.IO.FileMode.Open))
    {
       request = driveService.Files.Create(fileMetadata, stream, mimeType);
       request.Fields = "id";
       request.Upload();
    }
    
    file = request.ResponseBody;
    
    
     var request = service.Files.Get(fileId);
                        var stream = new System.IO.MemoryStream();
    
                        request.MediaDownloader.ProgressChanged +=
                            (IDownloadProgress progress) =>
                            {
                                switch (progress.Status)
                                {
                                    case DownloadStatus.Downloading:
                                        {                                       
                                            break;
                                        }
                                    case DownloadStatus.Completed:
                                        {
                                            var bytearray = stream.ToArray(); 
                                         System.IO.File.WriteAllBytes(SavedLocation, bytearray );
                                            break;
                                        }
                                    case DownloadStatus.Failed:
                                        {                                       
                                            break;
                                        }
                                }
                            };
                        request.Download(stream);