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 = "[email protected]";
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?