Reputation: 331
Updated Code that works for user account creation using Directory API: Adding user to OU using directory API still doesn't work.
String serviceAccountEmail = "[email protected]";
X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "secret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[]
{
DirectoryService.Scope.AdminDirectoryUser
},
User = "[email protected]",
}.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:
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.
Upvotes: 0
Views: 390
Reputation: 3845
for #1 You can download the library using NuGet and as you mentioned you could then reference it in you project.
for #2 when creating a user the method would be similar to the one you used before, you will have to give the user information. The method will create the POST call automatically.
If you wanted to call the API directly through a http request, you would need to specify the POST method. But in this case the library takes care of that part so you can focus on just providing the necessary information.
I know this example is for java but as the sintaxis is similar to c#, it may help you see how to call the api methods
Hope this helps.
Upvotes: 1