您好
我正在尝试在特定用户帐户下创建文件。
用户帐户位于我的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进行了身份验证,即上传文件的位置。
问题
我需要能够创建/复制此文件给特定用户 我的域名。并使该文件的用户所有者。
感谢所有帮助。
旧帖子参考
答案 0 :(得分:3)
我会选择Marcel Balk anwser。
这样我们就会使用假冒!
我们可以使用服务帐户进行身份验证,然后使用我们域中的任何电子邮件帐户并上传文件。 上传的文件将显示在我的文件下的用户云端硬盘中(不与我共享!) 文件也归模拟帐户所有。 在一个小控制台应用程序中测试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
namespace UseWebServiceMethod
{
class Program
{
// Goto https://admin.google.com/AdminHome?chromeless=1#OGX:ManageOauthClients
// Login met admin account => ****@***.***
// Add/edit Client Name (Use Client ID of service account here) => 2***8.apps.googleusercontent.com
// One or More API Scopes => https://www.googleapis.com/auth/drive
public const string ImpersonatedAccountEmail = "****@***.***";
public const string ServiceAccountEmail = "2******8@developer.gserviceaccount.com";
public const string Key = @"C:\Users\Wouter\Desktop\GoogleTester\GoogleTester\A****c.p12";
public const string FileToUpload = @"C:\Users\Wouter\Desktop\GoogleTester\GoogleTester\whakingly.txt";
public const string ApplicationName = "A***l";
static void Main()
{
var certificate = new X509Certificate2(Key, "notasecret", X509KeyStorageFlags.Exportable);
var initializer = new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = new[] { DriveService.Scope.Drive },
User = ImpersonatedAccountEmail
};
var credential = new ServiceAccountCredential(initializer.FromCertificate(certificate));
var driveService = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName
});
// Show all files
var list = driveService.Files.List().Execute();
if (list.Items != null)
foreach (var fileItem in list.Items)
{
Console.WriteLine(fileItem.Title + " - " + fileItem.Description);
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
// Upload a new file
File body = new File();
body.Title = "whakingly.txt";
body.Description = "A whakingly (that a new word I created just there) file thats uploaded with impersonation";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes(FileToUpload);
var stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = driveService.Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
// Show all files
var list2 = driveService.Files.List().Execute();
if (list2.Items != null)
foreach (var fileItem in list2.Items)
{
Console.WriteLine(fileItem.Title + " - " + fileItem.Description);
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
}
}
答案 1 :(得分:1)
您可以通过将用户添加到代码中来轻松完成此操作。 查看我添加的用户。
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},
User="someuser@somedomain.someextension" //Add this to your code!
}.FromCertificate(certificate));
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyApplicationName",
});
return service;
}
然后转到admin.google.com - >安全 - >高级设置 - >管理oauth客户端访问
将客户端ID添加到客户端名称,并将范围(http://www.googleapis.com/auth/drive)添加到"一个或多个API范围字段"