【发布时间】:2014-06-24 12:40:37
【问题描述】:
我正在开发 WPF 应用程序,在此我正在实现具有上传和下载功能的 Google Drive API。上传工作正常,但我在下载文档时遇到问题。我在https://developers.google.com/drive/web/manage-downloads查看了谷歌文档中的代码
public static System.IO.Stream DownloadFile(IAuthenticator authenticator, 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;
}
}
但是根据这个文件 “https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis/Apis/Authentication/IAuthenticator.cs?r=e6585033994bfb3a24d4c140db834cb14b9738b2” 它显示“不再支持 IAuthenticator”,因此上述代码不起作用。
我尝试使用 UserCredential,但它抛出“远程服务器返回错误:(401) 未授权。”。
所以请在 WPF 应用程序中提供相同的代码,或者我如何从 Google 驱动器下载任何类型的文件。
【问题讨论】:
标签: c# google-drive-api