Created — forked from anonymous/partner.java

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View partner.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
 
<%@page import="net.oauth.http.HttpClient"%><%@ page language="java" import="client.PartnerHttpClientPool,java.io.*, java.net.*,java.util.*,java.security.*,net.oauth.OAuth, net.oauth.OAuthAccessor, net.oauth.OAuthConsumer, net.oauth.OAuthMessage, net.oauth.OAuthServiceProvider, net.oauth.signature.RSA_SHA1, net.oauth.client.OAuthClient,java.util.*,java.math.BigInteger,javax.xml.parsers.DocumentBuilder,javax.xml.parsers.DocumentBuilderFactory,javax.xml.xpath.XPath,javax.xml.xpath.XPathConstants,javax.xml.xpath.XPathExpression,javax.xml.xpath.XPathFactory,org.w3c.dom.Document,org.w3c.dom.NodeList,org.xml.sax.InputSource,net.oauth.client.httpclient4.HttpClient4" %>
 
<%
 
OAuthServiceProvider xeroPartnerProvider =
new OAuthServiceProvider(
"https://api-partner.network.xero.com/oauth/RequestToken",
"https://api.xero.com/oauth/Authorize",
"https://api-partner.network.xero.com/oauth/AccessToken");
 
String consumerKey = "CONSUMERKEY";
String consumerSecret = "CONSUMERSECRET";
 
String apiURL="https://api-partner.network.xero.com/api.xro/2.0/";
 
 
InputStream oauthPKCS12Stream = new FileInputStream("c:/certificate/public_privatekey.pfx");
String oauthPKCS12Password = "xero";
InputStream clientCertPKCS12Stream = new FileInputStream("c:/certificate/PKCS12.p12");
String clientCertPKCS12Password = "xero";
 
KeyStore oauthKeyStore = KeyStore.getInstance("PKCS12");
oauthKeyStore.load(oauthPKCS12Stream, null);
PrivateKey oauthKey = null;
for (Enumeration e = oauthKeyStore.aliases(); e.hasMoreElements(); ) {
String alias = (String)e.nextElement();
if (oauthKeyStore.isKeyEntry(alias)) {
oauthKey = (PrivateKey)oauthKeyStore.getKey(alias, oauthPKCS12Password.toCharArray());
}
}
 
KeyStore entrustStore = KeyStore.getInstance("PKCS12");
entrustStore.load(clientCertPKCS12Stream, clientCertPKCS12Password.toCharArray());
 
OAuthConsumer consumer = new OAuthConsumer("https://www.xero.com", consumerKey, consumerSecret,
xeroPartnerProvider);
consumer.setProperty(RSA_SHA1.PRIVATE_KEY, oauthKey);
consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.RSA_SHA1);
 
 
OAuthAccessor accessor = new OAuthAccessor(consumer);
accessor.accessToken = consumerKey;
accessor.tokenSecret = consumerSecret;
 
OAuthClient client = new OAuthClient(
new HttpClient4(new client.PartnerHttpClientPool(entrustStore, clientCertPKCS12Password)));
 
List<Map.Entry> p = new ArrayList<Map.Entry>(1);
p.add(new OAuth.Parameter(OAuth.OAUTH_CALLBACK,
"https://www.xero.com"));
 
OAuthMessage m = client.getRequestTokenResponse(accessor,null,p);
//client.getRequestToken(accessor);
 
String authorizationURL = OAuth.addParameters(
accessor.consumer.serviceProvider.userAuthorizationURL,
OAuth.OAUTH_CALLBACK, "https://www.xero.com",
OAuth.OAUTH_TOKEN, accessor.requestToken
);
 
response.sendRedirect(authorizationURL);
 
//out.println("Authorization URL: "+authorizationURL);
//client.getAccessToken(accessor,OAuthMessage.GET,null);
//out.println("See the access token " + accessor.accessToken);
 
//OAuthMessage m = client.invoke(accessor,OAuthMessage.GET,apiURL+"Organisation",null);
//out.println("See the organisation selected " + OAuthMessage.readAll(m.getBodyAsStream(),"UTF-8"));
%>

Hi Ronan,
Please help me to get "client.PartnerHttpClientPool" package?

There you go:

public class PartnerHttpClientPool implements HttpClientPool {

    private HttpClient httpClient;

    public PartnerHttpClientPool(KeyStore entrustStore, String password) 
        throws KeyManagementException,
            NoSuchAlgorithmException,
            KeyStoreException,
            UnrecoverableKeyException {

        SSLContext sslcontext = SSLContexts.custom()
            .loadKeyMaterial(entrustStore, password.toCharArray())
            .build();
        httpClient = HttpClients.custom()
            .setSslcontext(sslcontext)
            .build();
    }

    public HttpClient getHttpClient(URL server) {
        return this.httpClient;
    }   
}

Hi Ronan, Do you have a guide that lists all the steps. I followed this but I get this error
[main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {s}->https://api-partner.network.xero.com:443: Connection reset
[main] INFO org.apache.http.impl.execchain.RetryExec - Retrying request to {s}->https://api-partner.network.xero.com:443
[main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {s}->https://api-partner.network.xero.com:443: Connection reset
[main] INFO org.apache.http.impl.execchain.RetryExec - Retrying request to {s}->https://api-partner.network.xero.com:443
[main] INFO org.apache.http.impl.execchain.RetryExec - I/O exception (java.net.SocketException) caught when processing request to {s}->https://api-partner.network.xero.com:443: Connection reset
[main] INFO org.apache.http.impl.execchain.RetryExec - Retrying request to {s}->https://api-partner.network.xero.com:443
E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.