2016-07-05 52 views
0

我使用Google.Apis.Admin.Directory.directory_v1.Data來管理谷歌用戶和組。我可以創建用戶和組,但是如何將新用戶插入新組? 這裏是我的代碼如何使用Google API將用戶插入組

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 = "[email protected]"; 
     username.GivenName = "GivenName"; 
     username.FamilyName = "FamilyName"; 
     user.Name = username; 
     user.Password = "Password"; 
     service.Users.Insert(user); 

     var group = new Group(); 
     group.Email = "[email protected]"; 
     group.Name = "GroupName"; 
     service.Groups.Insert(group); 

    } 

回答

0

添加Execute()和工作對我來說

service.Users.Insert(user).Execute() 
相關問題