我遵循了这个例子:
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客户端库
我缺少什么?
答案 0 :(得分:2)
不幸的是,我遇到了您所描述的相同问题,并且API文档没有提供有关IAuthenticator是什么的足够信息。正如rajanmhr47所说,它不再受支持,将被删除。
解决方案如下:
假设您有一个用于向Google云端硬盘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;
}
答案 1 :(得分:1)
在此link中,提到IAuthenticator不再受支持,它将在1.7.0-beta中删除。考虑使用新的Google.Apis.Auth NuGet包中的UserCredential或ServiceAccountCredential,它支持.NET 4,.NET for Windows,商店应用,Windows Phone 7.5和8以及便携式类库