2

尝试创建文件夹并设置其权限时出现以下错误。它可以在不传递文件元数据中的权限的情况下工作。

但我想创建对特定用户具有权限的文件夹,每个用户都有自己的 g-mail 帐户。我怎样才能做到这一点?谢谢!

错误信息:

编译/执行时的异常消息

编码:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace DriveQuickstart
{
  class Program
  {
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/drive-dotnet-quickstart.json
    static string[] Scopes = { DriveService.Scope.Drive };
    static string ApplicationName = "Drive API .NET Quickstart";

    static void Main(string[] args)
    {
      UserCredential credential;

      using (var stream =
      new FileStream("client_secret.json", FileMode.Open, FileAccess.ReadWrite))
      {
        string credPath = System.Environment.GetFolderPath(
        System.Environment.SpecialFolder.Personal);

        credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");

        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)).Result;
        Console.WriteLine("Credential file saved to: " + credPath);
      }

      // Create Drive API service.
      var service = new DriveService(new BaseClientService.Initializer()
      {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
      });

      Permission perm = new Permission();

      perm.Type = "user";
      perm.EmailAddress = "anotheruser@gmail.com";
      perm.Role = "owner";

      IList<Permission> perms = new List<Permission>();

      perms.Add(perm);

      var fileMetadata = new Google.Apis.Drive.v3.Data.File()
      {
        Name = "Invoices",
        MimeType = "application/vnd.google-apps.folder",
        Description = "Blah blah " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second,
        Permissions = perms
      };

      var request = service.Files.Create(fileMetadata);
      request.Fields = "id";
      var file = request.Execute();
      Console.WriteLine("Folder ID: " + file.Id);

      Console.Read();

    }
  }
}
4

2 回答 2

4

正如您在文档中看到的那样,该Permissions[]属性指示为writable字段。因此,在文件创建之前填充Permissions[]资源主体不是正确的方法;就像错误表明“资源主体包含不可直接写入的字段”一样。

那么如何创建共享文件夹呢?

您必须首先创建文件夹,然后使用它的Id.

您可以将代码修改为:

var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
    Name = "Invoices",
    MimeType = "application/vnd.google-apps.folder",
    Description = "Blah blah " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second
};

Permission perm = new Permission();
perm.Type = "user";
perm.EmailAddress = "anotheruser@gmail.com";
perm.Role = "owner";

var request = service.Files.Create(fileMetadata);
request.Fields = "id";

try 
{
    service.Permissions.Create(perm, request.Execute().Id).Execute(); //Creating Permission after folder creation.
}
catch (Exception e) 
{
    Console.WriteLine("An error occurred: " + e.Message);
}
于 2017-03-11T17:58:08.307 回答
1

确保您已通过正确的身份验证。请参阅此链接

当您想要访问 Google Drive API 时,身份验证是一切的关键。如果您希望能够访问数据,则需要进行身份验证。有两种类型的身份验证 OAuth2 允许您访问其他用户的数据,以及可以设置为访问您自己的数据的服务帐户。

您可以按照本教程中的示例代码:Google Drive 文件权限

/// <summary>
  /// Insert a new permission.
  /// </summary>
  /// <param name="service">Drive API service instance.</param>
  /// <param name="fileId">ID of the file to insert permission for.</param>
  /// <param name="who">
  /// User or group e-mail address, domain name or null for "default" type.
  /// </param>
  /// <param name="type">The value "user", "group", "domain" or "default".</param>
  /// <param name="role">The value "owner", "writer" or "reader".</param>
  /// <returns>The inserted permission, null is returned if an API error occurred</returns>
  public static Permission InsertPermission(DriveService service, String fileId, String who,String type, String role) {
    Permission newPermission = new Permission();
    newPermission.Value = value;
    newPermission.Type = type;
    newPermission.Role = role;
    try {
      return service.Permissions.Insert(newPermission, fileId).Execute();
    } catch (Exception e) {
      Console.WriteLine("An error occurred: " + e.Message);
    }
    return null;
  }
于 2017-03-11T11:21:07.560 回答