什么是Google.Apis.Authentication的命名空间;

时间:2015-07-12 07:19:13

标签: c# .net google-api google-drive-api google-api-dotnet-client

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

我在nuget中下载了名为“Google”的所有内容,但没有运气。

知道它可以藏在哪里吗?感谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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