I am trying to call the AdminService API to manage my domain's groups such adding new group members, create new groups etc. , but I'm stuck with the request to get all the users' of my domain. Here is the code:
public static class MembersSample
{
static void Main(string[] args)
{
String serviceAccountEmail = "*****@*****.iam.gserviceaccount.com";
var certificate = new X509Certificate2(@"pathofthefile.p12", "secret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] {
DirectoryService.Scope.AdminDirectoryUser,
DirectoryService.Scope.AdminDirectoryGroup,
DirectoryService.Scope.AdminDirectoryDomain,
DirectoryService.Scope.AdminDirectoryGroupMember },
User = "domainmanageremail"
}.FromCertificate(certificate));
var dirservice = new DirectoryService(new Google.Apis.Services.BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "my application name",
});
var listReq = dirservice.Users.List();
listReq.Domain = "my domain address";
Users allUsers = listReq.Execute();
int counter = 0;
foreach (User myUser in allUsers.UsersValue)
{
Console.WriteLine("*" + myUser.PrimaryEmail);
counter++;
}
Console.WriteLine(counter);
Console.ReadKey();
}
I am getting this error ;
Unhandled Exception: Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method.", Uri:""
My service account role is Service Account User, and my role is Service Account Admin in this project. Also, I did authorization part for the service account with the DirectoryService.Scope.AdminDirectoryUser scope (Domain Wide Delegation).
-Is this scope is wrong or do I need additional one to manage groups and members?
Thanks for any help!