I'm attempting to connect to Java (JAR) libraries to communicate with the "QuickBooks Merchant Services API".
Each call to the API requires an OAuth2 access token. My first step is to use the Java SDK that QuickBooks provides to get the access token.
I downloaded the "jar-with-dependencies.jar" option from this page. According to Maven, 6.0.1 is the current version.
https://search.maven.org/search?q=a:oauth2-platform-api
I created a folder named "java" off of the root of the web site and stored this file in that folder.
oauth2-platform-api-6.0.1-jar-with-dependencies.jar
In Application.cfc, I added this line of code:
<!--- Application.cfc snippet --->
<cfset this['javaSettings'] = {
LoadPaths = ["#expandPath('/java')#"]
,loadColdFusionClassPath = true
,reloadOnChange = true
,watchInterval = 100
,watchExtensions = "jar,class,xml"
}>
In a test.cfm file I have the following lines of code in a function:
variables['oauth2Config'] = createObject("java", "com.intuit.oauth2.config.OAuth2Config").OAuth2ConfigBuilder(arguments.clientId, arguments.clientSecret).callDiscoveryAPI(Environment.SANDBOX).buildConfig();
In an example on the developer.intuit.com web site, the Java code looks like this:
OAuth2Config oauth2Config = new OAuth2Config.OAuth2ConfigBuilder("clientId", "clientSecret").callDiscoveryAPI(Environment.SANDBOX).buildConfig();
When I run the code, I get the following error message.
Class not found: com.intuit.oauth2.config.OAuth2Config
I have swapped out the "LoadPaths" key with several different variants but all result in the same error.
LoadPaths = ["/java"]
LoadPaths = ["./java"]
LoadPaths = ["./java/oauth2-platform-api-6.0.1-jar-with-dependencies.jar"]
I have tried modifying the initialization call:
local['oauth2Config'] = createObject("java", "OAuth2Config").OAuth2ConfigBuilder(arguments.clientId, arguments.clientSecret).callDiscoveryAPI(Environment.SANDBOX).buildConfig();
That resulted in basically the same kind of error:
Class not found: OAuth2Config
I'm pretty sure that I'm doing something wrong in the this.javaSettings struct in Application.cfc. I was hoping that the Java JAR approach would be easier, but I may end up using the REST API that Intuit has. I still have to use OAuth2 and I'm not sure if the cfoauth tag/function will work with this system.
Let me know if you see anything that will help me get past this problem.
CF can't find OAuth2ConfigBuilder
because it's inner class, which requires a slightly different syntax:
createObject("java", "com.intuit.oauth2.config.OAuth2Config$OAuth2ConfigBuilder");
Environment
is also a class within the jar. You must create a reference to it before using the constant Environment.SANDBOX
Even after fixing those issues, a brief test with CF2016 threw a LinkageError due to the inclusion of slf4j within the custom jar, while also loading the jars from CF/lib
(which includes slf4j too). Switching to loadColdFusionClassPath = false
seemed to get past that error.
java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of coldfusion/runtime/java/JavaDynamicClassLoader) ...
Application.cfc
this['javaSettings'] = {
LoadPaths = ["#expandPath('/java')#"]
,loadColdFusionClassPath = false
, ...
};
Code:
variables['environ'] = createObject("java", "com.intuit.oauth2.config.Environment");
variables['builder'] = createObject("java", "com.intuit.oauth2.config.OAuth2Config$OAuth2ConfigBuilder");
variables['oauth2Config'] = variables['builder'].init( arguments.clientId, arguments.clientSecret).
callDiscoveryAPI( variables['environ'].SANDBOX ).buildConfig();