Search code examples
.netgoogle-directory-api

Google Admin SDK Directory API .Net Quick start guide


I am looking to explore the Google Admin SDK Directory API. Is there a Quick start guide or tutorial available. I have looked at the documentation there are quick start guides for java and python but not .Net. I help would be greatly appreciated.


Solution

  • The first step is to have a Service account with Domain-Wide Delegation Authority. The link below shows how to https://developers.google.com/admin-sdk/directory/v1/guides/delegation

    It say to add the service account to the "Manage third party OAuth Client access" in advanced setting but i had "Manage OAuth Client access" that works as well

    String serviceAccountEmail = "......@developer.gserviceaccount.com";
    X509Certificate2 certificate = new X509Certificate2(@"C:\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
    ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = new[]
                        {
                            DirectoryService.Scope.AdminDirectoryUser
                        },
                        User = "admin@domain.com"
                    }.FromCertificate(certificate));
    
                    var ser = new DirectoryService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Get it to work",
                    });
    
                    User newuserbody = new User();
                    UserName newusername = new UserName();
                    newuserbody.PrimaryEmail = "jack@domain.com";
                    newusername.GivenName = "jack";
                    newusername.FamilyName = "black";
                    newuserbody.Name = newusername;
                    newuserbody.Password = "password";
    
                    User results = ser.Users.Insert(newuserbody).Execute();