gpt4 book ai didi

c# - 使用google drive SDK下载文件

转载 作者:行者123 更新时间:2023-11-30 22:04:50 25 4
gpt4 key购买 nike

我遵循了这个例子:

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 APIs Client Library

Google.api.drive.v2 Client Library

我错过了什么?

最佳答案

不幸的是,我遇到了与您描述的相同的问题,并且 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;
}

关于c# - 使用google drive SDK下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24853876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com