How to I get a list of user accounts with the google directory API?

2.1k views Asked by At

Here is my code:

String key = Console.ReadLine();    //input oauth2.0 key from google
Console.WriteLine(key);
        Google.Apis.Admin.Directory.directory_v1.DirectoryService service = new Google.Apis.Admin.Directory.directory_v1.DirectoryService(new BaseClientService.Initializer
        {

        ApplicationName = "MY APPLICATION",
        ApiKey = key,


        });
Google.Apis.Admin.Directory.directory_v1.UsersResource.GetRequest u = service.Users.Get("[email protected]");
        Console.WriteLine(u.UserKey);


        Google.Apis.Admin.Directory.directory_v1.Data.User response = await u.ExecuteAsync();
Console.WriteLine(response);

Even though I have an OAuth2.0 key "key" from Google, I get an error message in reply saying something along the lines of error: login required. How should I change the code so that I can get (and eventually set) user information without such an error?

EDIT: My scope for the authorization key is the global user scope.

1

There are 1 answers

0
Piyush Jain On

I presume that you are using the Directory API ON Java. Follow this link, https://developers.google.com/admin-sdk/directory/v1/quickstart/java , it is the documentation of the Directory API of Google, and they have given the example for listing users.