Skip to content

Commit 35cdf78

Browse files
SidneyAllenkullfar
authored andcommitted
Xero API for oAuth2 and example code
1 parent bc85f4a commit 35cdf78

3 files changed

Lines changed: 167 additions & 23 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.github.scribejava.apis;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
import com.github.scribejava.core.oauth2.clientauthentication.ClientAuthentication;
5+
import com.github.scribejava.core.oauth2.clientauthentication.RequestBodyAuthenticationScheme;
6+
7+
/**
8+
* Xero.com Api
9+
*/
10+
public class XeroApi20 extends DefaultApi20 {
11+
12+
protected XeroApi20() {
13+
}
14+
15+
private static class InstanceHolder {
16+
private static final XeroApi20 INSTANCE = new XeroApi20();
17+
}
18+
19+
public static XeroApi20 instance() {
20+
return InstanceHolder.INSTANCE;
21+
}
22+
23+
@Override
24+
public String getAccessTokenEndpoint() {
25+
return "https://identity.xero.com/connect/token";
26+
}
27+
28+
@Override
29+
protected String getAuthorizationBaseUrl() {
30+
return "https://login.xero.com/identity/connect/authorize";
31+
}
32+
33+
@Override
34+
public ClientAuthentication getClientAuthentication() {
35+
return RequestBodyAuthenticationScheme.instance();
36+
}
37+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.github.scribejava.apis.examples;
2+
3+
import java.util.Random;
4+
import java.util.Scanner;
5+
import com.github.scribejava.core.builder.ServiceBuilder;
6+
import com.github.scribejava.core.model.OAuth2AccessToken;
7+
import com.github.scribejava.core.model.OAuthRequest;
8+
import com.github.scribejava.core.model.Response;
9+
import com.github.scribejava.core.model.Verb;
10+
import com.github.scribejava.core.oauth.OAuth20Service;
11+
import java.io.IOException;
12+
import java.util.concurrent.ExecutionException;
13+
14+
/*
15+
* This full example needs a library to parse
16+
* the JSON response and obtain a Xero Tenant Id
17+
* in order to access protected resources.
18+
*
19+
* If you add the following dependency, you can then
20+
* uncomment the rest of the example code to access
21+
* the Xero Organisation and other protected resources
22+
*
23+
* <dependency>
24+
<groupId>com.googlecode.json-simple</groupId>
25+
<artifactId>json-simple</artifactId>
26+
<version>1.1.1</version>
27+
</dependency>
28+
*
29+
*/
30+
31+
/*
32+
import org.json.simple.JSONArray;
33+
import org.json.simple.JSONObject;
34+
import org.json.simple.parser.JSONParser;
35+
import org.json.simple.parser.ParseException;
36+
*/
37+
38+
public class XeroExample {
39+
40+
private static final String NETWORK_NAME = "Xero";
41+
private static final String PROTECTED_RESOURCE_URL = "https://api.xero.com/connections";
42+
private static final String PROTECTED_ORGANISATION_URL = "https://api.xero.com/api.xro/2.0/Organisation";
43+
44+
private XeroExample() {
45+
}
46+
47+
@SuppressWarnings("PMD.SystemPrintln")
48+
public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
49+
// Replace these with your client id, secret and redirect uri
50+
final String clientId = "your client id";
51+
final String clientSecret = "your client secret";
52+
final String callback = "your redirect uri";
53+
final String secretState = "secret" + new Random().nextInt(999_999);
54+
final OAuth20Service service = new ServiceBuilder(clientId)
55+
.apiSecret(clientSecret)
56+
.defaultScope("openid email profile offline_access accounting.settings accounting.transactions") // replace with desired scope
57+
.callback(callback)
58+
.build(Xero20Api.instance());
59+
final Scanner in = new Scanner(System.in, "UTF-8");
60+
61+
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
62+
System.out.println();
63+
64+
// Obtain the Authorization URL
65+
System.out.println("Fetching the Authorization URL...");
66+
final String authorizationUrl = service.getAuthorizationUrl(secretState);
67+
System.out.println("Got the Authorization URL!");
68+
System.out.println("Now go and authorize ScribeJava here:");
69+
System.out.println(authorizationUrl);
70+
System.out.println("And paste the authorization code here");
71+
System.out.print(">>");
72+
final String code = in.nextLine();
73+
System.out.println();
74+
75+
System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
76+
System.out.print(">>");
77+
final String value = in.nextLine();
78+
if (secretState.equals(value)) {
79+
System.out.println("State value does match!");
80+
} else {
81+
System.out.println("Ooops, state value does not match!");
82+
System.out.println("Expected = " + secretState);
83+
System.out.println("Got = " + value);
84+
System.out.println();
85+
}
86+
87+
System.out.println("Trading the Authorization Code for an Access Token...");
88+
final OAuth2AccessToken accessToken = service.getAccessToken(code);
89+
System.out.println("Got the Access Token!");
90+
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
91+
System.out.println();
92+
93+
// GET the Xero Tenant ID
94+
System.out.println("Getting Xero tenant id...");
95+
final OAuthRequest requestConn = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
96+
requestConn.addHeader("Accept", "application/json");
97+
service.signRequest(accessToken.getAccessToken(), requestConn);
98+
final Response responseConn = service.execute(requestConn);
99+
System.out.println();
100+
System.out.println(responseConn.getCode());
101+
System.out.println(responseConn.getBody());
102+
/*
103+
JSONParser parser = new JSONParser();
104+
JSONArray jsonArray = null;
105+
try {
106+
jsonArray = (JSONArray) parser.parse(responseConn.getBody());
107+
} catch (ParseException e) {
108+
e.printStackTrace();
109+
}
110+
111+
JSONObject jsonObject = (JSONObject) jsonArray.get(0);
112+
System.out.println("Your Xero tenant id is ...." + jsonObject.get("tenantId"));
113+
114+
// GET protected Resource
115+
System.out.println("Now we're going to access a protected resource...");
116+
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_ORGANISATION_URL);
117+
request.addHeader("xero-tenant-id",jsonObject.get("tenantId").toString());
118+
service.signRequest(accessToken.getAccessToken(), request);
119+
final Response response = service.execute(request);
120+
121+
// Now let's go and ask for a protected resource!
122+
System.out.println("Got it! Lets see what we found...");
123+
System.out.println();
124+
System.out.println(response.getCode());
125+
System.out.println(response.getBody());
126+
*/
127+
System.out.println();
128+
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
129+
}
130+
}

src/main/java/org/scribe/builder/api/XeroApi20.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)