使用服务帐户将 MS Word docx 文件加载到 Google 驱动器时出现问题
Problem loading MS Word docx file to Google Drive with service account
尝试通过 Google Drive API v3 上传 docx 文件时,成功完成。我不明白为什么在尝试打开 docx 时出现错误。
我们使用 Google.Apis.Drive.v3 Version="1.49.0.2065" 程序包并使用 google 服务帐户进行身份验证。这是我们的代码示例:
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-dotnet-quickstart.json
static string[] Scopes = { DriveService.Scope.DriveFile, DriveService.Scope.Drive, DriveService.Scope.DriveAppdata, DriveService.Scope.DriveMetadata };
static string ApplicationName = "Drive API .NET Quickstart";
static void Main(string[] args)
{
var GoogleCred = GoogleCredential.FromFile("online-contract-236111-13ccc07ef347.json").CreateScoped(Scopes);
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = GoogleCred,
ApplicationName = ApplicationName,
});
var ids = service.Files.GenerateIds().Execute();
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File();
// fileMetadata.Name = @"excel1.xlsx";
fileMetadata.Name = @"КП.docx";
// fileMetadata.Name = @"present1.pptx";
string fileId = string.Empty;
// useing(var fileStream = File.Open(@"c:\temp\present.pptx", FileMode.Open))
using (var fileStream = System.IO.File.Open(@"c:\temp\Компред САЦ 2017.docx", FileMode.Open))
// using (var fileStream = File.Open(@"c:\temp\excel1.xlsx", FileMode.Open))
{
var fileUpload = service.Files.Create(fileMetadata, fileStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// var fileUpload = service.Files.Create(fileMetadata, fileStream, "application/vnd.openxmlformats-officedocument.presentationml.presentation");
// var fileUpload = service.Files.Create(fileMetadata, fileStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
fileUpload.Fields = "id";
IUploadProgress progress = fileUpload.Upload();
if (progress.Status == UploadStatus.Failed)
{
Console.WriteLine(progress.Exception);
}
else
{
fileId = fileUpload.ResponseBody.Id;
Console.WriteLine("File ID: " + fileId);
}
}
Console.ReadKey();
var permission = new Google.Apis.Drive.v3.Data.Permission();
permission.Type = "anyone";
permission.Role = "writer";
var perm = service.Permissions.Create(permission, fileId);
perm.Execute();
Console.WriteLine("Premission created!");
Console.ReadKey();
Process.Start("explorer.exe", "https://docs.google.com/document/d/" + fileId + "/edit");
// Process.Start("explorer.exe", "https://docs.google.com/presentation/d/" + fileId + "/edit");
// Process.Start("explorer.exe", "https://docs.google.com/spreadsheets/d/" + fileId + "/edit");
Console.WriteLine("Redirected to browser!");
Console.ReadKey();
}
}
但是,在Google帐户(Google Chrome)中使用浏览器和身份验证时,文件可以正常打开,没有任何错误。当我们使用 Google Drive 时,我们会在我们的驱动器上创建文件,并让用户在 public 访问中通过 fileId 编辑和使用它。
请问,我们到底有什么问题?
感谢 DalmTo,来自评论。问题出在 google 文件格式中。在上传 docx 文件之前需要将其转换为 google doc 文件格式,例如:
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File();
fileMetadata.Name = @"КП.docx";
fileMetadata.MimeType = "application/vnd.google-apps.document";
尝试通过 Google Drive API v3 上传 docx 文件时,成功完成。我不明白为什么在尝试打开 docx 时出现错误。
我们使用 Google.Apis.Drive.v3 Version="1.49.0.2065" 程序包并使用 google 服务帐户进行身份验证。这是我们的代码示例:
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-dotnet-quickstart.json
static string[] Scopes = { DriveService.Scope.DriveFile, DriveService.Scope.Drive, DriveService.Scope.DriveAppdata, DriveService.Scope.DriveMetadata };
static string ApplicationName = "Drive API .NET Quickstart";
static void Main(string[] args)
{
var GoogleCred = GoogleCredential.FromFile("online-contract-236111-13ccc07ef347.json").CreateScoped(Scopes);
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = GoogleCred,
ApplicationName = ApplicationName,
});
var ids = service.Files.GenerateIds().Execute();
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File();
// fileMetadata.Name = @"excel1.xlsx";
fileMetadata.Name = @"КП.docx";
// fileMetadata.Name = @"present1.pptx";
string fileId = string.Empty;
// useing(var fileStream = File.Open(@"c:\temp\present.pptx", FileMode.Open))
using (var fileStream = System.IO.File.Open(@"c:\temp\Компред САЦ 2017.docx", FileMode.Open))
// using (var fileStream = File.Open(@"c:\temp\excel1.xlsx", FileMode.Open))
{
var fileUpload = service.Files.Create(fileMetadata, fileStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// var fileUpload = service.Files.Create(fileMetadata, fileStream, "application/vnd.openxmlformats-officedocument.presentationml.presentation");
// var fileUpload = service.Files.Create(fileMetadata, fileStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
fileUpload.Fields = "id";
IUploadProgress progress = fileUpload.Upload();
if (progress.Status == UploadStatus.Failed)
{
Console.WriteLine(progress.Exception);
}
else
{
fileId = fileUpload.ResponseBody.Id;
Console.WriteLine("File ID: " + fileId);
}
}
Console.ReadKey();
var permission = new Google.Apis.Drive.v3.Data.Permission();
permission.Type = "anyone";
permission.Role = "writer";
var perm = service.Permissions.Create(permission, fileId);
perm.Execute();
Console.WriteLine("Premission created!");
Console.ReadKey();
Process.Start("explorer.exe", "https://docs.google.com/document/d/" + fileId + "/edit");
// Process.Start("explorer.exe", "https://docs.google.com/presentation/d/" + fileId + "/edit");
// Process.Start("explorer.exe", "https://docs.google.com/spreadsheets/d/" + fileId + "/edit");
Console.WriteLine("Redirected to browser!");
Console.ReadKey();
}
}
但是,在Google帐户(Google Chrome)中使用浏览器和身份验证时,文件可以正常打开,没有任何错误。当我们使用 Google Drive 时,我们会在我们的驱动器上创建文件,并让用户在 public 访问中通过 fileId 编辑和使用它。
请问,我们到底有什么问题?
感谢 DalmTo,来自评论。问题出在 google 文件格式中。在上传 docx 文件之前需要将其转换为 google doc 文件格式,例如:
Google.Apis.Drive.v3.Data.File fileMetadata = new Google.Apis.Drive.v3.Data.File();
fileMetadata.Name = @"КП.docx";
fileMetadata.MimeType = "application/vnd.google-apps.document";