Using Google's Directory API for .Net?

前端 未结 1 1172
感动是毒
感动是毒 2021-01-25 17:44

I am trying to create my first console app using Google\'s Directory API for .Net.

I have a code based in a Google\'s sample. It shows me a couple of errors, one

相关标签:
1条回答
  • 2021-01-25 18:18

    have to have a service account with Perform Google Apps Domain-Wide Delegation of Authority as defined in

    https://developers.google.com/admin-sdk/directory/v1/guides/delegation

    it say to add it in the "Manage third party OAuth Client access" i had "Manage OAuth Client access "

     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();
    
    0 讨论(0)
提交回复
热议问题