StringBuilder requestPostUrl = new StringBuilder(getAccessTokenURL());
// adding the oauth_verifier to the request.
requestPostUrl.append("?");
requestPostUrl.append(Configuration.OAUTH_VERIFIER).append('=')
.append(URLEncoder.encode(verifierCode, "UTF-8"));
HttpPost method = new HttpPost(requestPostUrl.toString());
// Collecting parameters for preparing the Signature
String consumerKey = getConsumerKey();
String requestToken = getRequestToken();
String nonce = getNonce();
String timeStamp = getTimestamp();
/*
* Generate a map of parameters which are required for creating signature. We are using a Linked
* HashMap to preserver the order in which parameters are added to the Map, as the parameters need
* to be sorted for Twitter Signature generation.
*/
LinkedHashMap<String, String> signatureParamsMap = new LinkedHashMap<String, String>();
signatureParamsMap.put(Configuration.CONSUMER_KEY, consumerKey);
signatureParamsMap.put(Configuration.NONCE, nonce);
signatureParamsMap.put(Configuration.OAUTH_TOKEN, requestToken);
signatureParamsMap.put(Configuration.SIGNATURE_METHOD, getSignatureMethod());
signatureParamsMap.put(Configuration.TIMESTAMP, timeStamp);
signatureParamsMap.put(Configuration.VERSION, Configuration.OAUTH_VERSION1);
String requestTokenSecret = getRequestTokenSecret();
String consumerSecret = getConsumerSecret();
String signature = HMACEncryptionUtility.generateHMACSignature(requestPostUrl.toString(),
method.getMethod(), consumerSecret, requestTokenSecret, signatureParamsMap);
// Preparing the Header for getting access token
StringBuilder headerStr = new StringBuilder();
headerStr.append("OAuth ").append(Configuration.CONSUMER_KEY).append("=\"").append(consumerKey)
.append("\"");
headerStr.append(",").append(Configuration.SIGNATURE_METHOD).append("=\"")
.append(getSignatureMethod()).append("\"");
headerStr.append(",").append(Configuration.TIMESTAMP).append("=\"").append(timeStamp)
.append("\"");
headerStr.append(",").append(Configuration.NONCE).append("=\"").append(nonce).append("\"");
headerStr.append(",").append(Configuration.VERSION).append("=\"")
.append(Configuration.OAUTH_VERSION1).append("\"");
// This is the request token which is obtained from getRequestTokenFromServer() method.
headerStr.append(",").append(Configuration.OAUTH_TOKEN).append("=\"").append(requestToken)
.append("\"");
headerStr.append(",").append(Configuration.SIGNATURE).append("=\"")
.append(URLEncoder.encode(signature, "UTF-8")).append("\"");
method.setHeader("Authorization", headerStr.toString());
method.setHeader("Authorization", headerStr.toString());
HttpResponse httpResponse = client.execute(method);
responseCode = httpResponse.getStatusLine().getStatusCode();
InputStream content = httpResponse.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));