简体   繁体   中英

How to insert user into group with Google api

I am using Google.Apis.Admin.Directory.directory_v1.Data to manage google users and groups. I can create user and group, but how to insert my new user in new group? Here is my code

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

        using (var stream =
            new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(
                System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/admin-directory_v1-dotnet-quickstart.json");

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new string[] { DirectoryService.Scope.AdminDirectoryGroup, DirectoryService.Scope.AdminDirectoryUser },
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
            Console.WriteLine("Credential file saved to: " + credPath);
        }

        var service = new DirectoryService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Directory API .NET Quickstart",
        });

        User user = new User();
        UserName username = new UserName();
        user.PrimaryEmail = "mail@gmail.com";
        username.GivenName = "GivenName";
        username.FamilyName = "FamilyName";
        user.Name = username;
        user.Password = "Password";
        service.Users.Insert(user);

        var group = new Group();
        group.Email = "group@gmail.com";
        group.Name = "GroupName";
        service.Groups.Insert(group);

    }

Use https://developers.google.com/admin-sdk/directory/v1/reference/members/insert Like role, add email as a parameter. Check the Members resource for more details.

添加Execute()并且对我有用

service.Users.Insert(user).Execute()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM