pes*_*ssi 11 c# asp.net google-api google-drive-api google-api-dotnet-client
我正在尝试使用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;
Run Code Online (Sandbox Code Playgroud)
我认为您调用 AuthorizeAsync() 是错误的。使用await 关键字并远程获取.Result。您必须将该方法标记为异步才能执行此操作。
我还建议使用 UploadAsync() 方法。您也不需要使用 ReadAllBytes() 将整个文件加载到内存中。只需使用 File.OpenRead() 并传递 FileStream 而不是 MemoryStream。
谷歌有一些示例代码: https ://developers.google.com/api-client-library/dotnet/guide/media_upload
| 归档时间: |
|
| 查看次数: |
5459 次 |
| 最近记录: |