In this article, we will learn about how to create a Google group and add a user to the Google group programmatically from .NET. This is the fourth article in the series of accessing Google API from .NET. This will be an advanced article because it will us Google Admin SDK to perform the administrative task on GSuite.
Now, as we will be doing Google administrative tasks programmatically which are available from the admin console, we would need to access it through an admin account who has access to the administrative privileges in GSuite account. For the propose of the demo, we will use service account (generic account) and then impersonate a user having GSuite admin access (who has logged into GSuite admin console at least once).
Google configuration
We won't go into details of how to create a service account and grant service access role as admin of GSuite. Below are some reference links.
- Create a service account
- Enable Admin SDK
- Enable Client Id on service account - We should get a JSON file, the store is somewhere safe we would need it in our code.
- Enable domain-wide delegation
.NET API
Get Google Admin Sdk nuget package from Visual Studio. Or you can directly download from this link, Please check for the latest version from above URL, you should get the option to Download package on the right side. Add references to below dlls.

Now to keep things simple, we will divide code into different functions for reusability and simplicity.
Get Directory Service
First, let us create a function to get the Directory Service Object. This function will be called in the below methods.
Parameters keyfilepath is the path of JSON file downloaded from console.developers.com. This function will validate the credentials and authenticate service account. If you see here we have used .CreatewithUser method, this is because we need to impersonate a admin user account to perform admin SDK operations.
- DirectoryService GetDirectoryService(string keyfilepath)
- {
- try
- {
- string[] Scopes = { DirectoryService.Scope.AdminDirectoryGroup, DirectoryService.Scope.AdminDirectoryGroupReadonly };
- GoogleCredential credential;
- using (var stream = new FileStream(keyfilepath, FileMode.Open, FileAccess.Read))
- {
- // As we are using admin SDK, we need to still impersonate user who has admin access
- // https://developers.google.com/admin-sdk/directory/v1/guides/delegation
- credential = GoogleCredential.FromStream(stream)
- .CreateScoped(Scopes).CreateWithUser("[email protected]");
- }
- // Create Directory API service.
- var service = new DirectoryService(new BaseClientService.Initializer()
- {
- HttpClientInitializer = credential,
- ApplicationName = "Gsuite Migration Tool",
- });
- return service;
- }
- catch (Exception ex)
- {
- throw;
- }
- }
Create Google Group
This is the main method which will create Google Group.




Sakshi JainPosted Nov 2, 2022, 1:32 PM
I really like this tutorial, as i am working on integrating Admin sdk users programatically. Could you please help me out with the same?
Abhishek SinghPosted May 17, 2022, 1:33 PM
Is There any way to do the same thing but without using G suite account ?
Ruhid RanaPosted May 4, 2021, 6:24 AM
Its not working. Would you help me please. My email [email protected]
Pritesh MaturkarPosted Jan 18, 2020, 11:31 AM
Very Nice any Useful Article .Thanks for Sharing.
Pankajkumar PatelPosted Sep 3, 2019, 12:09 AM
Good one ...