【发布时间】:2014-09-30 10:46:23
【问题描述】:
您好
我目前正在尝试在特定用户帐户下创建文件。
该用户帐户位于我的 Google 域中。
对于 Oauth,我使用服务帐户。
DriveService()
private static DriveService Service()
{
var certificate = new X509Certificate2(Constants.P12, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(Constants.ServiceAccountEmail)
{
Scopes = new[] {
DriveService.Scope.Drive,
}
}.FromCertificate(certificate));
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApplicationName",
});
return service;
}
创建文件
private static void CreateDriveDocument(string title)
{
Console.WriteLine("Google - Create New Document:" + Environment.NewLine);
File body = new File();
body.Title = title;
body.Description = "A test document";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes(@"C:\myDoc.txt");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
//Callin the service at "Service()"
FilesResource.InsertMediaUpload request = Service().Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
}
所以在这里我创建了一个文件,因为我使用 ServiceAccount 进行了身份验证,这就是文件上传的地方。
问题
我需要能够将此文件创建/复制给特定用户 我的域名。并让该用户成为文件的所有者。
感谢所有帮助。
旧帖子参考
【问题讨论】:
标签: c# .net google-drive-api google-oauth ownership