Google Drive SDK为用户创建文件
本文关键字:创建 文件 用户 Drive SDK Google | 更新日期: 2023-09-27 18:17:06
你好
我目前正在尝试创建一个特定用户帐户下的文件。
用户帐户位于我的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进行了身份验证,这就是文件上传的地方。
我需要能够创建/复制这个文件到一个特定的用户内我的域。并使该用户成为文件的所有者。
旧文章参考
TransferOwnership Using Google SDK
我选Marcel Balk的答案
这样我们将使用模拟!
我们可以使用服务帐户进行身份验证,然后使用我们域中的任何电子邮件帐户并上传文件。上传的文件将在我的文件下的用户驱动器中可见(不是与我共享!)文件也属于假冒的帐户。在一个小的控制台应用程序中测试:
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();
}
}
}
您可以通过将用户添加到代码中轻松地做到这一点。查看我添加的用户
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范围字段"