Skip to content

Instantly share code, notes, and snippets.

@IndiceeCoder
Created July 19, 2011 22:32
Show Gist options
  • Save IndiceeCoder/1093909 to your computer and use it in GitHub Desktop.
Save IndiceeCoder/1093909 to your computer and use it in GitHub Desktop.
Java Example - Print list of user's reports
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
public class ListReports {
private static CommonsHttpOAuthConsumer commonsHttpConsumer;
private static final String CONSUMERKEY = "67523805-e3fc-4620-a4bc-b62ae4bc2b1a";
private static final String CONSUMERSECRETE = "787c3c200403e2592b5d931fb3cc3e94";
private static final String TOKENKEY = "67523805-e3fc-4620-a4bc-b62ae4bc2b1a";
private static final String TOKENSECRETE = "787c3c200403e2592b5d931fb3cc3e94";
private static String host = "https://secure.indicee.com";
public static void main(String args[]) throws Exception {
//oauth initialization
DefaultOAuthConsumer oconsumer = new DefaultOAuthConsumer(CONSUMERKEY, CONSUMERSECRETE);
oconsumer.setTokenWithSecret(TOKENKEY, TOKENSECRETE);
commonsHttpConsumer = new CommonsHttpOAuthConsumer(oconsumer.getConsumerKey(), oconsumer.getConsumerSecret());
commonsHttpConsumer.setTokenWithSecret(oconsumer.getToken(), oconsumer.getTokenSecret());
System.out.println(listReports());
}
public static String listReports() throws Exception {
HttpGet httpGet = new HttpGet(host + "/api/reports.xml");
// init a http client
DefaultHttpClient httpClient = new DefaultHttpClient();
SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//authenticate the request with oauth
commonsHttpConsumer.sign(httpGet);
// send the data to get the response
HttpResponse httpResponse = httpClient.execute(httpGet);
int status = httpResponse.getStatusLine().getStatusCode();
if (status != 200) {
throw new Exception("Error retrieving reports list.");
}
return httpResponse.getEntity().getContent().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment