【问题标题】:Using google drive SDK to download file使用谷歌驱动SDK下载文件
【发布时间】:2014-07-20 19:02:19
【问题描述】:

我遵循了这个例子:

using Google.Apis.Authentication;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;

......
......


/// <summary>
        /// Download a file and return a string with its content.
        /// </summary>
        /// <param name="authenticator">
        /// Authenticator responsible for creating authorized web requests.
        /// </param>
        /// <param name="file">Drive File instance.</param>
        /// <returns>File's content if successful, null otherwise.</returns>
        private System.IO.Stream DownloadFile(IAuthenticator authenticator, Google.Apis.Drive.v2.Data.File file)
        {
            if (!String.IsNullOrEmpty(file.DownloadUrl))
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
                        new Uri(file.DownloadUrl));
                    authenticator.ApplyAuthenticationToRequest(request);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        return response.GetResponseStream();
                    }
                    else
                    {
                        Console.WriteLine(
                            "An error occurred: " + response.StatusDescription);
                        return null;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return null;
                }
            }
            else
            {
                // The file doesn't have any content stored on Drive.
                return null;
            }
        }

但是无法找到使用Google.Apis.Authentication 的命名空间 所以无法解析入参IAuthenticator authenticator

我已经从 Nuget 安装了两个

Google API 客户端库

Google.api.drive.v2 客户端库

我错过了什么?

【问题讨论】:

    标签: c# .net asp.net-mvc google-api google-drive-api


    【解决方案1】:

    不幸的是,我遇到了与您描述的相同的问题,并且 API 文档没有提供足够的信息来说明 IAuthenticator 是什么。正如 rajanmhr47 所说,它不再受支持,将被删除。

    解决方法如下:

    假设您有一个用于向 Google Drive API 服务器进行身份验证的 DriveService 实例变量,您可以像这样使用它来下载您有权访问的文件:

    注意:这些方法不执行任何错误检查。

    异步版本:

    public static async Task<byte[]> DownloadFile(DriveService driveService, string fileId)
    {
        var file = await driveService.Files.Get(fileId).ExecuteAsync();
        return await driveService.HttpClient.GetByteArrayAsync(file.DownloadUrl);
    }
    

    同步版本:

    public static byte[] DownloadFile(DriveService driveService, string fileId)
    {
        var file = driveService.Files.Get(fileId).Execute();
        return driveService.HttpClient.GetByteArrayAsync(file.DownloadUrl).Result;
    }
    

    【讨论】:

      【解决方案2】:

      link 中提到不再支持 IAuthenticator,它将在 1.7.0-beta 中删除。考虑使用新的 Google.Apis.Auth NuGet 包中的 UserCredential 或 ServiceAccountCredential,它还支持 .NET 4、.NET for Windows、商店应用、Windows Phone 7.5 和 8 以及可移植类库

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-24
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        相关资源
        最近更新 更多