This is an old revision of the document!


Integrating QuickBooks Using Java

QuickBooks Online Edition

NOTE: WARNING! This is *super* sloppy code. You'll want to clean it up a bit before you use it!

import java.net.*;
import java.io.*;
import javax.net.ssl.*;

public class Test {

	protected static String _appID = "134476472";
	
	protected static String _appLogin = "test.www.yourdomain.com";
	
	protected static String _connTicket = "TGT-47-1sRm2nXMVfm$n8hb2MZfVQ";
	
	protected static String _appURL = "https://webapps.quickbooks.com/j/AppGateway";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// First, we need to sign on to QBOE
		String xml = "<?xml version=\"1.0\" ?>" + 
		"<?qbxml version=\"6.0\"?>" + 
		"<QBXML>" + 
		"	<SignonMsgsRq>" + 
		"		<SignonDesktopRq>" + 
		"			<ClientDateTime>2007-01-02T01:02:35</ClientDateTime>" + 
		"			<ApplicationLogin>" + Test._appLogin + "</ApplicationLogin>" + 
		"			<ConnectionTicket>" + Test._connTicket + "</ConnectionTicket>" + 
		"			<Language>English</Language>" + 
		"			<AppID>" + Test._appID + "</AppID>" + 
		"			<AppVer>1</AppVer>" + 
		"		</SignonDesktopRq>" + 
		"	</SignonMsgsRq>" + 
		"</QBXML>";
		
		String out = "";
		try {
			out = Test._doRequest(xml);
			System.out.println(out);
		}
		catch (Exception ex) {
			System.out.println("Something bad happened: " + ex.getMessage());
		}
		
		// Parse out the connection ticket
		String sessTicket = out.substring(291, 330);
		System.out.println("Session ticket: " + sessTicket);
		
		// Send an actual request
		String xml2 = "<?xml version=\"1.0\" ?>" +  
"<?qbxml version=\"6.0\"?>" +  
"<QBXML> " + 
"	<SignonMsgsRq>" + 
"		<SignonTicketRq>" +  
"			<ClientDateTime>2006-09-20T15:49:26</ClientDateTime>" +  
"			<SessionTicket>" + sessTicket + "</SessionTicket>" + 
"			<Language>English</Language> " + 
"			<AppID>" + Test._appID + "</AppID>" + 
"			<AppVer>1</AppVer>" +  
"		</SignonTicketRq>" +  
"	</SignonMsgsRq>" +  
"	<QBXMLMsgsRq onError=\"continueOnError\">" +  
"		<CustomerQueryRq requestID=\"2\" />" +  
"	</QBXMLMsgsRq>" +  
"</QBXML>";
		
		try {
			System.out.println(Test._doRequest(xml2));		
		}
		catch (Exception ex) {
			System.out.println("Something bad happened: " + ex.getMessage());
		}
	}

	protected static String _doRequest(String xml) throws Exception {
		String xmlOut = null;
		       
		       try 
		       {
		          URL url= new URL(Test._appURL);
		          HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
		          connection.setDoOutput(true);
		          connection.setDoInput(true);

		          connection.setRequestProperty("Content-Type", "application/x-qbxml");
		          
		          PrintWriter out = new PrintWriter(connection.getOutputStream());
		          out.println(xml); //XML Input
		          out.close();
		          BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		          String inputLine = "";
		          xmlOut = "";
		          StringBuffer strOut = new StringBuffer();
		          
		          while ((inputLine = in.readLine()) != null)
		          {
		              strOut.append(inputLine);
		          }
		          xmlOut = strOut.toString();

		          in.close();
		       }
		       catch(ConnectException conEx)
		       {
		          System.out.println("Connection is unavailable. (ConnectException in SecureEnterpriseSocketSession class) " + conEx);
		          throw new Exception(conEx.getMessage());
		       }
		       catch(MalformedURLException malformedURLEx)
		       {
		          System.out.println("Invalid URL. Cannot Connect. (MalformedURLException in SecureEnterpriseSocketSession class) " + malformedURLEx);
		          throw new Exception(malformedURLEx.getMessage());
		       }
		       catch(IOException ioEx)
		       {
		          System.out.println("Invalid URL. Cannot Connect. (IOException in SecureEnterpriseSocketSession class) " + ioEx);
		          throw new Exception(ioEx.getMessage());
		       }
		       return xmlOut;
	}
}
quickbooks_integration_java.1304541337.txt.gz · Last modified: 2013/01/21 12:44 (external edit)