Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用服务帐户将MS Word docx文件加载到Google Drive时出现问题_C#_Google Drive Api_Google Api Dotnet Client - Fatal编程技术网

C# 使用服务帐户将MS Word docx文件加载到Google Drive时出现问题

C# 使用服务帐户将MS Word docx文件加载到Google Drive时出现问题,c#,google-drive-api,google-api-dotnet-client,C#,Google Drive Api,Google Api Dotnet Client,当尝试通过GoogleDriveAPIv3上传docx文件时,成功完成。我不明白为什么在尝试打开docx时会出现错误 我们使用Google.api.Drive.v3 Version=“1.49.0.2065”包,auth使用Google服务帐户。下面是我们的代码示例: class Program { // If modifying these scopes, delete your previously saved credentials // at ~/.credential

当尝试通过GoogleDriveAPIv3上传docx文件时,成功完成。我不明白为什么在尝试打开docx时会出现错误

我们使用Google.api.Drive.v3 Version=“1.49.0.2065”包,auth使用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)中使用浏览器和身份验证时,文件可以正常打开,没有任何错误。当我们使用谷歌硬盘时,我们会在硬盘上创建文件,并允许用户通过fileId在公共访问中编辑和使用它


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

感谢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";

错误消息用英语说什么?错误消息是什么?在问题本身中发布文本,而不是带有非英语信息框的屏幕截图<代码>当我们在浏览器中使用浏览器并在谷歌帐户中进行身份验证时,这是意料之中的-除非文档被明确共享,否则谷歌文档不允许匿名访问。您是否将文件转换为谷歌文档格式?在英文中,加载文件失败-重试或报告错误Dalmto,不,我们不转换文件,情况如何?