Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IntuitDeveloperRelations/e484bf749e37b0da1fb3 to your computer and use it in GitHub Desktop.
Save IntuitDeveloperRelations/e484bf749e37b0da1fb3 to your computer and use it in GitHub Desktop.
package com.poc.oauth;
/**
* Created by Manas Mukherjee on 12/8/2014.
* Intuit Developer Group
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.http.HttpParameters;
import org.apache.log4j.Logger;
public class OAuthTest {
public static final Logger LOG = Logger.getLogger(OAuthTest.class);
public static final String REQUEST_TOKEN_URL = "https://oauth.intuit.com/oauth/v1/get_request_token";
public static final String ACCESS_TOKEN_URL = "https://oauth.intuit.com/oauth/v1/get_access_token";
public static final String AUTHORIZE_URL = "https://appcenter.intuit.com/connect/begin";
OAuthConsumer ouathconsumer;
public OAuthTest(String consumerkey, String consumersecret) {
this.ouathconsumer = new DefaultOAuthConsumer(consumerkey, consumersecret);
}
public void getRequestToken(String callbackURL){
OAuthProvider provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL);
try {
String authUrl = provider.retrieveRequestToken(this.ouathconsumer, callbackURL);
System.out.println("AuthURL - " + authUrl);
System.out.println("RequestToken - " + this.ouathconsumer.getToken());
System.out.println("RequestTokenSecret - " + this.ouathconsumer.getTokenSecret());
} catch (Exception e) {
e.printStackTrace();
}
}
public void getAccessToken(String verifierCode,
String requestToken, String requestTokenSecret) {
String accessToken = "";
String accessTokenSecret = "";
try {
this.ouathconsumer.setTokenWithSecret(requestToken, requestTokenSecret);
HttpParameters additionalParams = new HttpParameters();
additionalParams.put("oauth_callback", OAuth.OUT_OF_BAND);
additionalParams.put("oauth_verifier", verifierCode);
this.ouathconsumer.setAdditionalParameters(additionalParams);
String signedURL = this.ouathconsumer.sign(ACCESS_TOKEN_URL);
URL url = new URL(signedURL);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
String accesstokenresponse = "";
if (urlConnection != null) {
BufferedReader rd = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
System.out.println(sb.toString());
}
rd.close();
accesstokenresponse = sb.toString();
}
if (accesstokenresponse != null) {
String[] responseElements = accesstokenresponse.split("&");
if (responseElements.length > 1) {
accessToken = responseElements[1].split("=")[1];
accessTokenSecret = responseElements[0].split("=")[1];
LOG.info("OAuth accessToken: " + accessToken);
LOG.info("OAuth accessTokenSecret: " + accessTokenSecret);
Map<String, String> accesstokenmap = new HashMap<String, String>();
accesstokenmap.put("accessToken", accessToken);
accesstokenmap.put("accessTokenSecret", accessTokenSecret);
}
}
} catch (Exception e) {
LOG.error(e.getLocalizedMessage());
}
}
public static void main(String args[]){
String consumerkey = "use your tokens";
String consumersecret = "use your tokens";
OAuthTest oAuthTest = new OAuthTest(consumerkey, consumersecret);
/*Step 1 - pass the callback URL*/
oAuthTest.getRequestToken("http://localhost:3000/accesstoken");
/*Step 2 - From the above step, you'll find the AuthrizedURL. Paste it in browser to complete the OAuth flow
*Your callback URL will be called by Intuit. Parse the verifierId which needs to be passed in the next step
*
*Sample Call back endpoint and Intuit's call with query parameters
*CallBack URL - http://localhost:3000/accesstoken
*Intuit's call to this URL with query params - http://localhost:3000/accesstoken?oauth_token=qyprdhEshUv92QB1WTDLCljcCtPgEry70xKXitgAwkYaLgsD&oauth_verifier=ddruqkh&realmId=1143337960&dataSource=QBO
*
**/
/*Step 3 - pass the callback URL
*
* Sample Call - oAuthTest.getAccessToken("ddruqkh", "qyprdhEshUv92QB1WTDLCljcCtPgEry70xKXitgAwkYaLgsD", "FiEPbtrBKBj0XQNrIKtsAibrpLmevvzwz5bJjAoG");
* */
//oAuthTest.getAccessToken("", "", "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment