【问题标题】:Problem loading MS Word docx file to Google Drive with service account使用服务帐户将 MS Word docx 文件加载到 Google Drive 时出现问题
【发布时间】:2023-04-07 00:31:05
【问题描述】:

当尝试通过 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 时,我们会在我们的驱动器上创建文件,并让用户通过 fileId 在公共访问中编辑和使用它。

请问,我们到底出了什么问题?

【问题讨论】:

  • 错误信息用英文怎么说?
  • 错误信息是什么?在问题本身中发布 text,而不是带有非英语消息框的屏幕截图。 when we use the browser and auth in google account in browser 这是预期的 - 除非明确共享文档,否则 Google 文档不允许匿名访问
  • 您是否将文件转换为 google doc 格式?
  • In English error is failed to load file - 重试或报错
  • DalmTo,不,我们不转换文件,怎么样?

标签: c# google-drive-api google-api-dotnet-client


【解决方案1】:

感谢来自 cmets 的 DalmTo。问题出在谷歌文件格式中。在上传 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";

More info in Google file format convert question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多