使用 C# 在共享驱动器中复制 google 文档文件

copying google doc file in shared drive using c#

我们使用 google 驱动器已经有一段时间了 select 我们一名员工拥有的现有文档,复制它,然后将数据合并到占位符字段中,然后下载 pdf文档的版本。它一直运行良好,只是现在其他员工想要访问创建的文档。所以当我们给他们一个 link 时,原来的员工必须给他们访问权限。我们希望将文档移动到共享驱动器,所有员工都可以看到共享驱动器中的任何内容。从我在 google 中找到的内容来看,我们似乎需要根据请求将 SupportsAllDrives 属性 设置为 true。但是,我在复制基本文档时创建的任何对象上都找不到 属性。因此,在尝试复制文件时,我一直从 google 收到 404。谁能建议如何让它工作?

var secrets = new ClientSecrets
{
    ClientId = GoogleCredentials.accesskey,
    ClientSecret = GoogleCredentials.secretkey
};

var refreshToken = _credService.GetRefreshToken();

if (string.IsNullOrEmpty(refreshToken))
{
    throw new Exception("Missing google refresh token for google doc processor task.");
}

var token = new TokenResponse { RefreshToken = refreshToken };

var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = secrets
}),
"user",
token);

var docService = new DocsService(new BaseClientService.Initializer
{
    HttpClientInitializer = credentials,
    ApplicationName = "Contract Merge"
});

var driveService = new DriveService(new BaseClientService.Initializer
{
    HttpClientInitializer = credentials,
    ApplicationName = "Contract Merge"
});

var newTitle = "Agreement for " + contract.FirstName + " " + contract.LastName + " " + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Year.ToString();
var newFile = new google.Apis.Drive.v2.Data.File { Title = newTitle };
var documentCopyFile = driveService.Files.Copy(newFile, GoogleConstants.TemplateDocId).Execute();

如您在 C# 库参考中所见,可选的查询参数如 supportsAllDrives(请参阅 query parameters on the API docs) is handled by a property of the CopyRequest class

因此,在构建 CopyRequest 之后,但在执行它之前,您必须将 属性 SupportsAllDrives 设置为 true,如下所示:

FilesResource.CopyRequest copyRequest = driveService.Files.Copy(newFile, GoogleConstants.TemplateDocId);
copyRequest.SupportsAllDrives = true;
copyRequest.Execute();

参考: