/Xero-Java

Java Client for Xero API. Supports Accounting API

Primary LanguageJavaMIT LicenseMIT

Xero-Java

This is the Xero Java SDK for the Xero API. Currently, supports Accounting API. All third party libraries dependencies managed with Maven

Xero App

You'll need to decide which type of Xero app you'll be building Private, Public, or Partner. Go to http://app.xero.com and login with your Xero user account to create an app.

Questions?

Watch this video walkthrough of how to setup Xero Java SDK in Eclipse.

Download Xero Java SDK

Add this dependency and repository to your POM.xml

<dependency>
  <groupId>com.xero</groupId>
  <artifactId>xero-java-sdk</artifactId>
  <version>0.0.7</version>
</dependency>

<repositories>
  <repository>
    <id>xero-java-mvn-repo</id>
    <url>https://raw.github.com/XeroAPI/Xero-Java/mvn-repo/</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

####Working with Gradle, sbt or leiningen? Head over to Jitpack.io and lookup http://github.com/XeroAPI/Xero-Java to have your dependency built off the master branch.

Configure

The Xero Java SDK depends on an external JSON file to configure values unique to your Application.

We include an Example App (in this repo) built using Eclipse. We started with a Maven Project and selected the maven-archetype-webapp with the setup Wizard. This created a web application structure good for use with Servlets & JSPs. By default a src/main/resources directory is added to the project. Place the config.json file you create in the resources directory.

The Xero Java SDK - Config.java class parses the JSON file from the resources directory using the following bit of code.

final ClassLoader loader = Config.class.getClassLoader();
URL path = loader.getResource("config.json");
File f = new File(path.getFile());

How to Create the config.json file

In a text editor, create a file called config.json (examples are below) Refer to Xero Developer Center Getting Started when you are ready to create a Xero App - this is how you'll create a Consumer Key and Secret. Private and Partner apps require a public/private key pair you'll create using OpenSSL. The private key should be exported as a pfx file and in our example we create a "certs" folder inside the resources folder and place it there.

Public Application

{ 
	"AppType" : "PUBLIC",
	"UserAgent": "Your App Name",
	"ConsumerKey" : "WTCXXXXXXXXXXXXXXXXXXXXXXKG",
	"ConsumerSecret" : "GJ2XXXXXXXXXXXXXXXXXXXXXXXXWZ",
	"CallbackBaseUrl" : "http://localhost:8080/myapp",
	"CallbackPath" : "/CallbackServlet"
}

Private Application

{ 
	"AppType" : "PRIVATE",
	"UserAgent": "Your App Name",
	"ConsumerKey" : "CW1XXXXXXXXXXXXXXXXXXXXXXXXYG",
	"ConsumerSecret" : "SRJXXXXXXXXXXXXXXXXXXXXXXXZEA6",
	"PrivateKeyCert" :  "certs/public_privatekey.pfx",
	"PrivateKeyPassword" :  "1234"
}

Partner Application

{ 
	"AppType" : "PARTNER",
	"UserAgent": "Your App Name",
	"ConsumerKey" : "FA6UXXXXXXXXXXXXXXXXXXXXXXRC7",
	"ConsumerSecret" : "7FMXXXXXXXXXXXXXXXXXXXXXXXXXCSA",
	"CallbackBaseUrl" : "http://localhost:8080/myapp",
	"CallbackPath" : "/CallbackServlet",
	"PrivateKeyCert" :  "certs/public_privatekey.pfx",
	"PrivateKeyPassword" :  "1234"
}

Optionals Attributes

  • UserAgent: for debugging by Xero API team (unique string)
  • Accept: format of data returned from API (application/xml or application/json) default is XML
  • ApiBaseUrl: base URL for API calls default is https://api.xero.com
  • ApiEndpointPath: path for API Calls default is /api.xro/2.0/
  • RequestTokenPath: path for Request Token default it /oauth/RequestToken
  • AuthenticateUrl: path for redirect to authorize default is /oauth/RequestToken
  • AccessTokenPath: path for Access Token default is https://api.xero.com/oauth/Authorize

Example App

This repo includes an Example App mentioned above. The file structure mirrors that of an Eclipse Maven Project with the maven-archetype-webapp

  • src/main/java contains the com.xero.example package and the servlets for handling oAuth and sample API calls
  • src/main/resource contains examples of config.json files
  • src/main/webapp contains index and callback JSP files along with web.xml mappings for Servlets

oAuth Flow

For Public & Partner Apps, you'll implement 3 legged oAuth - Private Apps can skip down to the Data Endpoints (your Consumer Key will act as your permenent Access Token)

private Config config = Config.getInstance(); 

// Start by requesting a temporary token from Xero
OAuthRequestToken requestToken = new OAuthRequestToken(config);
requestToken.execute();

// DEMONSTRATION ONLY - Store in Cookie - you can extend TokenStorage
// and implement the save() method for your database
TokenStorage storage = new TokenStorage();
storage.save(response,requestToken.getAll());

//Build the Authorization URL and redirect User
OAuthAuthorizeToken authToken = new OAuthAuthorizeToken(requestToken.getTempToken());
response.sendRedirect(authToken.getAuthUrl());	

In your callback Servlet you'll read the query params and swap your temporary for your 30 min access token. In our example, we forward the user to the callback.jsp if successful.

private Config config = Config.getInstance(); 


// DEMONSTRATION ONLY - retrieve TempToken from Cookie
TokenStorage storage = new TokenStorage();

// retrieve OAuth verifier code from callback URL param
String verifier = request.getParameter("oauth_verifier");

// Swap your temp token for 30 oauth token
OAuthAccessToken accessToken = new OAuthAccessToken(config);
accessToken.build(verifier,storage.get(request,"tempToken"),storage.get(request,"tempTokenSecret")).execute();

// Check if your Access Token call successful
if(!accessToken.isSuccess())
{
	storage.clear(response);
	request.getRequestDispatcher("index.jsp").forward(request, response);
}
else 
{
	// DEMONSTRATION ONLY - Store in Cookie - you can extend TokenStorage
	// and implement the save() method for your database
	storage.save(response,accessToken.getAll());			
	request.getRequestDispatcher("callback.jsp").forward(request, response);			
}		

In your callback.jsp, you can have a link to access some data resources.

Data Endpoints

The Xero Java SDK contains XeroClient which has helper methods to perform (Create, Read, Update and Delete) actions on each endpoints. Once you instantiate XeroClient, you'll use Xero API schema classes to interact with Java Objects.

import com.xero.api.*;
import com.xero.model.*;



// Get Xero API Resource - DEMONSTRATION ONLY get token from Cookie
TokenStorage storage = new TokenStorage();
String token = storage.get(request,"token");
String tokenSecret = storage.get(request,"tokenSecret");

// For Private Apps the token is your consumerKey and the tokenSecret is your consumerSecret
// You can get these values out of the config object above
XeroClient client = new XeroClient();
client.setOAuthToken(token, tokenSecret);

// Get All Contacts
List<Contact> contactList = client.getContacts();
System.out.println("How many contacts did we find: " + contactList.size());
				
/* CREATE ACCOUNT */
ArrayOfAccount accountArray = new ArrayOfAccount();
Account account = new Account();
account.setCode("66000");
account.setName("Office Expense");
account.setType(AccountType.EXPENSE);
accountArray.getAccount().add(account);
List<Account> newAccount = client.createAccounts(accountArray);
			
/* READ ACCOUNT using a WHERE clause */
List<Account> accountWhere = client.getAccounts(null,"Type==\"BANK\"",null);

/* READ ACCOUNT using the ID */
List<Account> accountList = client.getAccounts();
Account accountOne = client.getAccount(accountList.get(0).getAccountID());
			
/* UPDATE ACCOUNT */
newAccount.get(0).setName("Entertainment");
newAccount.get(0).setStatus(null);
List<Account> updateAccount = client.updateAccount(newAccount);

/* DELETE ACCOUNT */
String status = client.deleteAccount(newAccount.get(0).getAccountID());

// GET INVOICE MODIFIED in LAST 24 HOURS
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, -1);
		    
List<Invoice> InvoiceList24hour = client.getInvoices(cal.getTime(),null,null);
System.out.println("How many invoices modified in last 24 hours?: " + InvoiceList24hour.size());

##Acknowledgement

Special thanks to Connectifier and Ben Mccann. Marshalling and Unmarshalling in XeroClient was derived and extended from Xero-Java-Client

##License

This software is published under the MIT License.

Copyright (c) 2016 Xero Limited

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.