3

I have been trying to create a script to use a service acct with Google's Directory API and having no luck. So far I have:

-Downloaded the Admin Directory JAR Files from here and put them all in my WEB-INF/lib dir

-Created a service acct according to their instructions for domain delegation – I generated a P12 file instead of JSON because that's what the instantiation code uses

-I converted the JAVA code on that same instruction page to ColdFusion so it looks like this

p12File = createObject( 'java', 'java.io.File' ).init("{my file path}");
accountId = JavaCast( "string", "XXX@XXX.iam.gserviceaccount.com" );

GoogleCredential = createObject( "java", "com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder" );
HttpTransport = createObject( "java", "com.google.api.client.http.HttpTransport" );
NetHttpTransport = createObject( "java", "com.google.api.client.http.javanet.NetHttpTransport" );
JsonFactory = createObject( "java", "com.google.api.client.json.JsonFactory" );
JacksonFactory = createObject( "java", "com.google.api.client.json.jackson2.JacksonFactory" );
Directory = createObject( "java", "com.google.api.services.admin.directory.Directory$Builder" );
DirectoryScopes = createObject( "java", "com.google.api.services.admin.directory.DirectoryScopes" );
Collections = createObject( "java", "java.util.Collections" );

credential = GoogleCredential
.setTransport( NetHttpTransport )
.setJsonFactory( JacksonFactory )
.setServiceAccountId( accountId )
.setServiceAccountScopes( Collections.singleton( DirectoryScopes.ADMIN_DIRECTORY_USER ) )
.setServiceAccountUser("XXX@XXX.org")
.setServiceAccountPrivateKeyFromP12File( p12File )
.build();

service = Directory.Init( NetHttpTransport, JacksonFactory, javaCast( "null", "" ) ).setHttpRequestInitializer( credential ).build();

I get a Coldfusion Error at my service=Directory.Init... line that reads "The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code. Null Pointers are another name for undefined values." and I don't understand why. All I've been able to figure out is that this is a rather generic error for what appears to be an unacceptable value for one of the variables.

TIA for any help you could offer

  • 1. Dump the `Directory` variable, and *each* of the variables in the init() call to see if any of them are null. 2. May be unrelated, but ... neither `NetHttpTransport` or `JacksonFactory` were initialized. Though normally CF invokes the no-arg constructor automatically when the first method is invoked, I do not know if that applies when passing an uninitialized object into a constructor. Might try invoking init() on them explicitly. – Leigh Jan 31 '17 at 21:24
  • ... also, you may want to split up the chained method calls if only to make it easier to troubleshoot problems and/or errors. – Leigh Feb 01 '17 at 01:47
  • Leigh - thanks for the help. Specifically adding the .init() to those 2 items appears to have done the trick. I now get back a service object which, when I dump it, displays the methods available. I say 'appears' because I'm mainly a CF guy, my JAVA skills are not very strong. When I try to use a method call like `service.groups().list()` - my results are empty when there s/b 2 groups returned. IDK if I'm using the object incorrectly, missing a step or google does not liking something. I'll have to do more testing and post a follow-up/new question. – Aces 'n Eights Feb 07 '17 at 18:40
  • 1
    Yes- I changed the DirectoryScopes constant to ADMIN_DIRECTORY_GROUP – Aces 'n Eights Feb 07 '17 at 19:58
  • Ah, okay. That makes sense. I will write up the init() suggestion in case it helps someone else getting a NPE. – Leigh Feb 07 '17 at 20:01

1 Answers1

1

(From the comments ...)

May be unrelated, but .. neither NetHttpTransport or JacksonFactory was initialized in the CF Code. Normally CF invokes the no-arg constructor automatically, when you first invoke a method on an un-initialized java object:

If you call a public non-static method on the object without first calling the init method, ColdFusion makes an implicit call to the default constructor.

However, I do not know if that same rule applies when passing an uninitialized object into a constructor. Try invoking init() on the objects explicitly to see if that resolves the NullPointerException:

NetHttpTransport = createObject( "java", "com.google.api.client.http.javanet.NetHttpTransport" ).init();
JacksonFactory = createObject( "java", "com.google.api.client.json.jackson2.JacksonFactory" ).init();
Leigh
  • 28,765
  • 10
  • 55
  • 103