【发布时间】:2015-07-01 16:02:05
【问题描述】:
我正在尝试使用来自 asp.net 应用程序的 Google Drive API 来上传文件。
问题:代码在本地工作,但上传到服务器时没有任何反应(页面一直在加载...没有显示同意屏幕。帮助 appreaciated。 “UploadedPdf”文件夹是可写的,并且client_secrets.json 存在于该文件夹中。
注意:我没有在服务器上安装任何东西,只是将包含 Google API dll 的所有文件上传到服务器的 bin 文件夹中。
UserCredential credential;
using (var filestream = new FileStream(Server.MapPath("UploadedPdf/client_secrets.json"),
FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(filestream).Secrets,
new[] { DriveService.Scope.Drive },
"user",
CancellationToken.None,
new FileDataStore(Server.MapPath("UploadedPdf/"))).Result;
}
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = "My document";
body.Description = "A test document";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes(Server.MapPath("UploadedPdf/sample.txt"));
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
【问题讨论】:
-
您是否在 Google 开发者控制台中将重定向 uri 添加到服务器?
-
是的,我确实添加了重定向网址。
-
转到浏览器控制台任何错误请更新?
-
检查您是否拥有在服务器中保存文件的完全权限
-
我真的很生气 Google 不能让这个 API 正常工作。它非常容易出错,很难学习,对于简单的操作来说非常复杂。我想使用云存储作为我的 Blob 存储 (DMS),在这种情况下真的很糟糕。只需 Google 一下,您就会发现数十个关于人们在使用 Drive API 的不同层次上遇到的问题。
标签: c# asp.net google-api google-drive-api google-api-dotnet-client