Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Google Drive API中设置文件夹权限_C#_.net_Google Drive Api_Folder Permissions - Fatal编程技术网

C# 在Google Drive API中设置文件夹权限

C# 在Google Drive API中设置文件夹权限,c#,.net,google-drive-api,folder-permissions,C#,.net,Google Drive Api,Folder Permissions,我在尝试创建文件夹并设置其权限时遇到以下错误。它将在不传递文件元数据中的权限的情况下工作 但我想创建对特定用户具有权限的文件夹,每个用户都有自己的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.

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

但我想创建对特定用户具有权限的文件夹,每个用户都有自己的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();

    }
  }
}
使用Google.api.Auth.OAuth2;
使用Google.api.Drive.v3;
使用Google.api.Drive.v3.Data;
使用Google.api.Services;
使用Google.api.Util.Store;
使用制度;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
命名空间驱动程序快速启动
{
班级计划
{
//如果修改这些作用域,请删除以前保存的凭据
//位于~/.credentials/drive-dotnet-quickstart.json
静态字符串[]范围={DriveService.Scope.Drive};
静态字符串ApplicationName=“Drive API.NET Quickstart”;
静态void Main(字符串[]参数)
{
用户凭证;
使用(var)流=
新的文件流(“client_secret.json”、FileMode.Open、FileAccess.ReadWrite))
{
字符串credPath=System.Environment.GetFolderPath(
系统、环境、专用文件夹、个人);
credPath=Path.Combine(credPath,“.credentials/drive dotnet quickstart.json”);
凭证=GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
范围,
“用户”,
取消令牌。无,
新文件数据存储(credPath,true))。结果;
Console.WriteLine(“凭证文件保存到:”+credPath);
}
//创建驱动器API服务。
var service=new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=ApplicationName,
});
权限perm=新权限();
perm.Type=“用户”;
perm.EmailAddress=”anotheruser@gmail.com";
perm.Role=“所有者”;
IList perms=新列表();
添加(perm);
var fileMetadata=new Google.api.Drive.v3.Data.File()
{
Name=“发票”,
MimeType=“application/vnd.google apps.folder”,
Description=“Blah Blah”+System.DateTime.Now.Hour+”:“+System.DateTime.Now.Minute+”:“+System.DateTime.Now.Second,
权限=烫发
};
var request=service.Files.Create(fileMetadata);
request.Fields=“id”;
var file=request.Execute();
Console.WriteLine(“文件夹ID:+file.ID”);
Console.Read();
}
}
}

确保您已通过正确的身份验证。看这个

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

您可以遵循本教程中的示例代码:

//
///插入新权限。
/// 
///驱动API服务实例。
///要为其插入权限的文件的ID。
/// 
///用户或组电子邮件地址、域名或“默认”类型为空。
/// 
///值“用户”、“组”、“域”或“默认值”。
///“所有者”、“作者”或“读者”的值。
///如果发生API错误,则返回插入的权限null
公共静态权限InsertPermission(DriveService服务、字符串文件ID、字符串用户、字符串类型、字符串角色){
权限newPermission=新权限();
newPermission.Value=Value;
newPermission.Type=Type;
newPermission.Role=Role;
试一试{
return service.Permissions.Insert(newPermission,fileId.Execute();
}捕获(例外e){
Console.WriteLine(“发生错误:+e.Message”);
}
返回null;
}
正如您在中所看到的,
权限[]
属性是表示为
可写的
字段。因此,在创建文件之前在资源体中填充
权限[]
,不是正确的方法;正如错误指示的“资源体包含不可直接写入的字段”

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

您必须首先创建文件夹,然后使用其
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);
}
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);
}