JoBe
JoBe

Reputation: 391

Has anyone got Google DirectoryService.Users.List().Execute(); to work in C#/.NET?

I've been trying to list users in my google apps domain for a while now. No problem in Python, but in C# i get an error message: An error has occured: Google.Apis.Requests.RequestError Bad Request [400] Errors [ Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global] ]

I'm no C# guru of any sorts but when I looked through the Google.Apis.Admin.directory_v1.cs - file it looked to me as if the UserResource ListRequest is wrong??? It's found on line 7349-7352 in the file. Anyone know's if it's not yet implemented in the API?

Edit: I start with why I THINK the code in Google.Apis.Admin.directory_v1.cs, lines 7349-7352 is wrong(as I mentioned - I'm not a C#-guru):

The Code:

 /// <summary>Retrieve either deleted users or all users in a domain (paginated)</summary>
    public virtual ListRequest List() {
        return new ListRequest(service);
}

Why I find it odd:

I can' see where to pass the customerid or domain as an argumant to this request, but in the APIs Explorer it's needed (otherwise I get the same error message as above, in my original post).

Edit : I looked a bit further down in the file and I guess that line 8904 and onwards is doing what I looked for earlier. My Bad!

But still I can't get my code to work?!?!?


And my code that won't work:

static void Main(string[] args)
    {
        // Display the header and initialize the sample.
        CommandLine.EnableExceptionHandling();
        Console.WriteLine("List users in a google apps domain!");
        Console.WriteLine("by Jonas Bergstedt 2013");

        // Get the domainname
        Console.Write("Domain: ");
        string domain = Console.ReadLine();

        // Register the authenticator.
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
        {
            ClientIdentifier = <myClientId>,
            ClientSecret = <myClientSecret>",
        };

        var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

        // Create the service.
        var service = new DirectoryService(new BaseClientService.Initializer()
        {
            Authenticator = auth,
            ApplicationName = "List Users",
            ApiKey = <myApiKey>
        });
        // Trying to add the domain
        service.Users.List().Domain = domain;

        Users results = service.Users.List().Execute();

        foreach (User list in results.UsersValue)
        {
            Console.WriteLine("- " + list.Name);
        }
    }
    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
    {
        // Get the auth URL:
        IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() });
        state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
        Uri authUri = arg.RequestUserAuthorization(state);

        // Request authorization from the user (by opening a browser window):
        Process.Start(authUri.ToString());
        Console.WriteLine();
        Console.Write("Authorization Code: ");
        string authCode = Console.ReadLine();

        // Retrieve the access token by using the authorization code:
        return arg.ProcessUserAuthorization(authCode, state);
    }
}

Upvotes: 1

Views: 1906

Answers (1)

peleyal
peleyal

Reputation: 3512

ListRequest had those properties. It looks like those properties aren't mandatory, so they aren't part of the constructor. You can do the following:

var listReq = service.Users.List();
listReq.Customer = "CUSTOMER_HERE";
listReq.Domain = "DOMAIN_HERE";
Users results = listReq.Execute();

Upvotes: 1

Related Questions