Updated Code that works for user account creation using Directory API: Adding user to OU using directory API still doesn't work.
String serviceAccountEmail = ".........@developer.gserviceaccount.com";
X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "secret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[]
{
DirectoryService.Scope.AdminDirectoryUser
},
User = "test@example.com",
}.FromCertificate(certificate));
var ser = new DirectoryService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Google Account",
});
try
{
var user = new Google.Apis.Admin.Directory.directory_v1.Data.User()
{
Name = new Google.Apis.Admin.Directory.directory_v1.Data.UserName()
{
GivenName = FirstName.Text,
FamilyName = LastName.Text
},
Password = password
};
User newUser = new User();
UserName newUserName = new UserName();
newUser.PrimaryEmail = Email.Text;
newUserName.GivenName = FirstName_txt.Text;
newUserName.FamilyName = LastName_txt.Text;
newUser.Name = newUserName;
newUser.Password = password;
User results = ser.Users.Insert(newUser).Execute();
}
We were using Google Apps Provisioning API for .Net code, but now we need to switch to directory API, I am following the steps mentioned in this link: https://developers.google.com/admin-sdk/directory/v1/get-start/getting-started
But I am confused for some of the steps mentioned here so, I have some questions if anyone can give me some idea.
Below is the code that was working in .NET to use the Google Apps Provisioning API which creates google account for user, adds user to Organization Unit, group. I need to achieve same goal using Directory API now:
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Google.GData.Apps;
using System.Text.RegularExpressions;
//Creating Google Account
AppsService appService = new AppsService("Mydomain", "AdminUsername", "AdminPassword");
try
{
//Create Google Account with the information we got through database
var account = appService.CreateUser(googleEmail, FirstName.Text, LastName.Text, password);
ResultsLabel.Text += "<br />Google Account created";
}
catch (Exception ex)
{
ResultsLabel.Text += "<br />Can't add this user to Google Account";
}
//Adding User to Organization Unit
OrganizationService service = new OrganizationService("mydomain", "applicationName");
try
{
service.setUserCredentials("AdminUsername", "AdminPassword");
service.UpdateOrganizationUser("GoogleCustomerId", Email.Text, "Staff", "/");
ResultsLabel.Text += "<br />Added to Organization Unit";
}
catch (Exception ex)
{
ResultsLabel.Text += "<br />Can't add to Organization Unit";
}
//Adding User to the group
appService.Groups.AddMemberToGroup(Email.Text, "Staff");
Here are my questions:
- In steps it is mentioned: Install Client Library using this link: https://developers.google.com/adminsdk/directory/v1/libraries
I downloaded .NET client library, but how to include that in my code I am not sure. In Google Apps Provisioning API I have used as References and I have used namespace, Example: using Google.GData.Apps. But in Directory API how to include this library that I have downloaded?
In Provisioning API I have used AppsService and created user like this:
var account = appService.CreateUser(googleEmail, FirstName.Text, LastName.Text, password);
But In Directory API we need to use POST request as mentioned in this link? https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#create_user
How to do POST request in my existing code like above, if anyone can give me example or idea that would be great.