使用服务帐户将MS Word docx文件加载到Google云端硬盘时出现问题 [英] Problem loading MS Word docx file to Google Drive with service account

查看:120
本文介绍了使用服务帐户将MS Word docx文件加载到Google云端硬盘时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试通过Google Drive API v3上传docx文件时,它会成功完成.我不明白为什么在尝试打开docx时出现错误.

When trying to upload a docx-file via Google Drive API v3, it completes with success. I can't understand why an error shows up when trying to open the docx.

我们使用Google.Apis.Drive.v3版本="1.49.0.2065"包和用于身份验证的Google服务帐户.这是我们的代码示例:

we use Google.Apis.Drive.v3 Version="1.49.0.2065" package and for auth use google service account. Here an example of our code:

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云端硬盘时,我们会在驱动器上创建文件,并允许用户通过fileId在公共访问权限中对其进行编辑和使用.

However, when using the browser and auth in Google Account (Google Chrome), the file opens fine, without any error. When we use Google Drive we create files on our drive and give users edit and use it in public access by fileId.

拜托,我们到底有什么问题?

Please, what is wrong in our end?

推荐答案

感谢DalmTo的评论.问题出在google文件格式.在上传docx文件之前,需要将其转换为google doc文件格式,例如:

Thanks to DalmTo, from comments. The problem were in google file format. Before upload docx file need convert it to google doc file format like:

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文件格式的信息会转换问题

这篇关于使用服务帐户将MS Word docx文件加载到Google云端硬盘时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆