Adding User to Organization Unit and Group using Google Apps Provisioning API

1k Views Asked by At

I have done coding in .Net to create google account using Google Apps Provisioning API. I am successful in creating Google Account but I am not being able to add user to particular Organization Unit and Group. I have achieved the goal of account creation like this:

//Creating Google Account

 AppsService appService = new AppsService("Mydomain", "AdminUsername", "AdminPassword");

         try
                {

                var account = appService.CreateUser(googleEmail, FirstName.Text, LastName.Text, password);

                }

                catch (Exception ex)
                {
                    ResultsLabel.Text += "<br />Can't create this Google Account";
                }

After user is added, it gets created under main domain "Users" in Google. But I have to put that user into the Organization unit where they belong to, here in my case, I need to put the user into "Staff" Organization Unit. I am not sure what applicationName means, I am just using the project solution name or am I not using correct name here? What does applicatinName mean, and what should I use? I am using CustomerID as "GoogleCustomerId" in following code that I got from Google for our domain. I have done coding like this which doesn't work for adding user to Organization Unit:

//Adding User to Organization Unit


                OrganizationService service = new OrganizationService("mydomain", "applicationName");

                service.setUserCredentials("AdminUsername", "AdminPassword");

                service.UpdateOrganizationUser("GoogleCustomerId", Email.Text, "Staff", "Users"); 

I get this exception with the above code to add user to Organization Unit:

Google.GData.Client.GDataRequestException was unhandled by user code
  HResult=-2146233088
  Message=Execution of request failed: https://apps-apis.google.com/a/feeds/orguser/2.0/C090ll5hh/[email protected]
  Source=Google.GData.Client
  ResponseString=<?xml version="1.0" encoding="UTF-8"?>
<AppsForYourDomainErrors>
  <error errorCode="1801" invalidInput="" reason="InvalidValue" />
</AppsForYourDomainErrors>

Here is my code to add user to Group but it is also not working, I need to add user to [email protected] group:

//Adding User to Group


   service.Groups.AddMemberToGroup("[email protected]", username);

Any idea on this one please?

Thanks

2

There are 2 best solutions below

0
TechPro On BEST ANSWER

After further research and making change in code adding to Organization Unit and Group is now working. I have to put "/" for "String oldOrgUnitPath" as it is the top organization unit where user is initially added.

//Adding User to Organization Unit

            OrganizationService service = new OrganizationService("mydomain", "applicationName");

            service.setUserCredentials("AdminUsername", "AdminPassword");

            service.UpdateOrganizationUser("GoogleCustomerId", Email.Text, "Staff", "/"); 

To make adding to group work I have to make this change in code. We shouldn't use @domain.com with group name and switching the position of Email id and group name worked.

//Adding User to Group

   service.Groups.AddMemberToGroup(Email.Text, "Staff");

Thanks

2
VicTheMagician On

Regarding to Google, this API is about to stop working in the end of April, 2015.

I've had a couple of problems with this API since last year and I have decided to take a look at the "new" API instead: https://developers.google.com/admin-sdk/directory/

Even if you solved this problem for now, I would recommend you to do check the new API if you want your application to run smoothly in the future.

Good luck!