Prerequisites:
Maven
Gradle
Install Jars Manually
pom.xml
file. Make sure to use the latest version of the SDK found here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!-- data jar --> <dependency> <groupId> com.intuit.quickbooks-online </groupId> <artifactId> ipp-v3-java-data </artifactId> <version> 6.0.7 </version> </dependency> <!-- devkit jar with dependencies--> <dependency> <groupId>com.intuit.quickbooks-online</groupId> <artifactId>ipp-v3-java-devkit</artifactId> <classifier>jar-with-dependencies</classifier> <version> 6.0.7 </version> </dependency> <!-- oauth jar with dependencies--> <dependency> <groupId>com.intuit.quickbooks-online</groupId> <artifactId>oauth2-platform-api</artifactId> <classifier>jar-with-dependencies</classifier> <version> 6.0.7 </version> </dependency> |
build.gradle
file.
For a Maven project, add the following dependencies to the pom.xml
file. Make sure to use the latest version of the SDK found here.
1 2 3 | compile("com.intuit.quickbooks-online:ipp-v3-java-data:6.0.7") compile (group: 'com.intuit.quickbooks-online', name: 'ipp-v3-java-devkit', version: '6.0.7', classifier: 'jar-with-dependencies') compile (group: 'com.intuit.quickbooks-online', name: 'oauth2-platform-api', version: '6.0.7', classifier: 'jar-with-dependencies') |
While we recommend using a package manager to track the dependencies in your application, it is possible to download and use the Java SDK manually by downloading a pre-built jar file. Click the download button next to the latest version of the artifact to download.
View Screenshot
View Screenshot
View Screenshot
View Screenshot
View Screenshot
View Screenshot
http://localhost:8080/oauth2redirect
. Set the Redirect URI in the Keys & OAuth tab with this URL.View Screenshot
1 2 3 4 5 6 7 8 | #OAuth2 App Configuration OAuth2AppClientId=add your clientId OAuth2AppClientSecret=add your clientSecret OAuth2AppRedirectUri=http://localhost:8080/oauth2redirect #QBO API endpoint IntuitAccountingAPIHost=https://sandbox-quickbooks.api.intuit.com |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public View connectToQuickBooks() { //initialize the config OAuth2Config oauth2Config = new OAuth2Config.OAuth2ConfigBuilder(clientId,clientSecret) .callDiscoveryAPI(Environment.SANDBOX) .buildConfig(); //generate csrf token String csrf = oauth2Config.generateCSRFToken(); //prepare scopes List<Scope> scopes = new ArrayList<Scope>(); scopes.add(Scope.Accounting); //prepare authorization url to intiate the oauth handshake return new RedirectView(oauth2Config.prepareUrl(scopes, redirectUri, csrf), true, true, false); } |
The code above:
OAuth2Config
object by providing the clientId
and clientSecret
and by specifying the environment (sandbox or production) to make the API calls.The above method uses the Spring framework for controller mapping but can be modified to match any MVC framework used in your application.
View Screenshot
After the user clicks the Authorize button, the request is sent to an Intuit server.
http://localhost:8080/oauth2redirect
.access_token
and refresh_token
using the Token Endpoint.The access_token
is used in an API request and the refresh_token
is used to get a fresh, short-lived access_token
after it expires.
Below is a code snippet; view complete code here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public String callBackFromOAuth() { //initialize the config OAuth2Config oauth2Config = new OAuth2Config.OAuth2ConfigBuilder(clientId,clientSecret) .callDiscoveryAPI(Environment.SANDBOX) .buildConfig(); //prepare OAuth2Platform client OAuth2PlatformClient client = new OAuth2PlatformClient(oauth2Config); //retrieve access token by calling the token endpoint BearerTokenResponse bearerTokenResponse = client.retrieveBearerTokens(authCode, redirectUri); return "connected"; } |
Once the access_token
is obtained, it can be used to make QuickBooks Online API calls. The method below illustrates how to make a call to the CompanyInfo endpoint, by performing the following:
DataService
object by passing the realmId
and accessToken
.executeQuery
method of the service class to get company information.The data query can be modified to execute any QuickBooks Online API supported query as a String in the sql
parameter in the snippet below. view complete code here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public String callQBOCompanyInfo() { //get DataService DataService service = helper.getDataService(realmId, accessToken); // get all companyinfo String sql = "select * from companyinfo"; QueryResult queryResult = service.executeQuery(sql); CompanyInfo companyInfo = (CompanyInfo) queryResult.getEntities().get(0); // process response ObjectMapper mapper = new ObjectMapper(); return mapper.writeValueAsString(companyInfo); } |