7

Using Google API .NET Client v1.3.0-beta.

I created a console application to test the basics of Google Admin Directory from the sample code for Service Accounts. This should give me a count of all email addresses in the domain.

using System;
using System.Collections.Generic;        
using DotNetOpenAuth.OAuth2;        
using Google.Apis.Admin.directory_v1;
using Google.Apis.Admin.directory_v1.Data;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Services;
using Google.Apis.Util;

namespace ConsoleApplication1
{
    class Program
    {
        private const string SERVICE_ACCOUNT_EMAIL = "012345678901@developer.gserviceaccount.com";
        private const string SERVICE_ACCOUNT_ID = "012345678901.apps.googleusercontent.com";
        private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"C:\ConsoleApplication1\randomlyassigned-privatekey.p12";    

        static void Main(string[] args)
        {
            X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
            var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
            {
                ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
                Scope = AdminService.Scopes.AdminDirectoryUser.GetStringValue()
            };

            var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
            var service = new AdminService(new BaseClientService.Initializer()
                {
                    Authenticator = auth
                });

            UsersResource.ListRequest listReq = service.Users.List();
            Users us = listReq.Fetch();
            Console.WriteLine(us.UsersValue.Count);
        }
    }
}

I get the following error when I try to Fetch() the list of users:

Google.Apis.Requests.RequestError
Bad Request [400]
Errors [
    Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global]
]

Any idea what I may be doing wrong here?

  • I just updated to v1.4.0-beta, and things have gotten worse. AdminService and ListRequest.Fetch() both seem to be missing. I tried switching to the new DirectoryService but can't figure out how to get the ListRequest into list Users. AND I get errors with the included libraries, like "Could not load file or assembly 'System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified." – David Templeton Jun 27 '13 at 18:39
  • ListRequest now has an Execute(). I updated the resources. Now I'm back to the same error. – David Templeton Jun 28 '13 at 13:09
  • Solved it using JoBe's suggestion below and by adding ServiceAccountUser to the provider. I got the idea for adding ServiceAccountUser by following JoBe's answers to [link](http://stackoverflow.com/questions/17750237/new-google-drive-directory-apis-error-out-bad-request/17811256#17811256), so thank you twice JoBe! – David Templeton Aug 07 '13 at 13:34

2 Answers2

4

I got some help sorting this out. To list users you have to pass domain or customer as an argument for your list-request. Use EITHER CUSTOMER or DOMAIN.

@peleyal helped me findig how to do that:

var listReq = service.Users.List();
listReq.Customer = "CUSTOMER_HERE";
listReq.Domain = "DOMAIN_HERE";
Users results = listReq.Execute(); 
JoBe
  • 391
  • 3
  • 12
  • No luck. But it did give me another error. Not Authorized to access this resource/api [403] – David Templeton Aug 06 '13 at 21:56
  • have you enabled the api in APIs console? – JoBe Aug 07 '13 at 05:09
  • Found it. I used your suggestion here and added ServiceAccountUser to the provider. It is working now. Thanks! – David Templeton Aug 07 '13 at 13:37
  • In addition to this, you can specify the string "my_customer" to list all users from ALL domains. `listReq.Customer = "my_customer"`; The documentation mentions this in a very vague way. https://developers.google.com/admin-sdk/directory/v1/guides/manage-users#get_all_users – Wilson Waters Aug 28 '13 at 05:37
  • What does "added ServiceAccountUser to the provider" mean, where did you do this? Also what is this "provider"? – Joshua G Mar 02 '15 at 18:59
3

I am also facing the same error [Bad Request] while loading users from domain using new Directory APIs.

To remove errors with the included libraries use the steps mentioned in https://code.google.com/p/google-api-dotnet-client/wiki/Build Also verify that you are using proper version of .Net and not client Profile.

Jeevan
  • 167
  • 2
  • 11