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

相關問題