0

我正在尝试使用this link使用dotnet从Google Drive下载文件。 问题是,我无法在nuget中找到这个命名空间 - 使用Google.Apis.Authentication; 。什么是Google.Apis.Authentication的名称空间;

我已经下载了在nuget中名称为“Google”的所有内容,但没有成功。

任何想法,它可以隐藏?谢谢

回答

0

要访问Google驱动器,您需要下载的唯一的块金包是PM> Install-Package Google.Apis.Drive.v2。它会自动添加你需要的任何东西。

我从驱动方式

/// <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; 
      } 
     } 

代码下载从google drive sample project

0

我认为,对你是一个更好的样本(在正式样品回购,https://github.com/google/google-api-dotnet-client-samples/blob/master/Drive.Sample/Program.cs#L154)撕开。有关媒体下载

... 
    await DownloadFile(service, uploadedFile.DownloadUrl); 
    ... 

    /// <summary>Downloads the media from the given URL.</summary> 
    private async Task DownloadFile(DriveService service, string url) 
    { 
     var downloader = new MediaDownloader(service); 
     var fileName = <PATH_TO_YOUR_FILE> 
     using (var fileStream = new System.IO.FileStream(fileName, 
      System.IO.FileMode.Create, System.IO.FileAccess.Write)) 
     { 
      var progress = await downloader.DownloadAsync(url, fileStream); 
      if (progress.Status == DownloadStatus.Completed) 
      { 
       Console.WriteLine(fileName + " was downloaded successfully"); 
      } 
      else 
      { 
       Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ", 
        fileName, progress.BytesDownloaded); 
      } 
     } 
    } 

更多的文档可以在这里找到: https://developers.google.com/api-client-library/dotnet/guide/media_download

相关问题