gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:40 25 4
gpt4 key购买 nike

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

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

错误信息:

Exception message upon compiling/executing

代码:

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();

}
}
}

最佳答案

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

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

您必须先创建文件夹,然后再创建 Create the Permission对于它,使用它的 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);
}

关于c# - 在 Google Drive API 中设置文件夹权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42726044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com