【问题标题】:Google Drive SDK create file for userGoogle Drive SDK 为用户创建文件
【发布时间】: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 进行了身份验证,这就是文件上传的地方。

问题

我需要能够将此文件创建/复制给特定用户 我的域名。并让该用户成为文件的所有者。

感谢所有帮助。

旧帖子参考

TransferOwnership Using Google SDK

【问题讨论】:

    标签: c# .net google-drive-api google-oauth ownership


    【解决方案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 范围字段”

    【讨论】:

    • 那不行,你要在输入用户帐号的同时验证服务帐号。
    【解决方案2】:

    我会和 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();
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多