summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-24 17:57:32 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-25 01:40:09 +0200
commita5a864e35426067ed3cd511e2a6a59446127b942 (patch)
tree5d7c60311bbc21f61a3f2987c0c9209a8a84f186
parent3178d4d0a92e4cfd2eff84a2a298acf514337633 (diff)
downloadTwitterDataAnalytics-a5a864e35426067ed3cd511e2a6a59446127b942.tar.gz
Implement OAuth OOB method
-rw-r--r--src/mining/OAuthRequester.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/mining/OAuthRequester.java b/src/mining/OAuthRequester.java
index 8421d42..d72ca2f 100644
--- a/src/mining/OAuthRequester.java
+++ b/src/mining/OAuthRequester.java
@@ -2,11 +2,14 @@ package mining;
import java.io.IOException;
import java.net.URLConnection;
+import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
+import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthException;
import org.json.JSONObject;
import support.ConsumerKeySecret;
+import utils.Configuration;
/**
* An API requester that uses OAuth to sign its requests.
@@ -21,6 +24,11 @@ public class OAuthRequester extends AbstractRequester {
private final OAuthConsumer consumer;
/**
+ * Instance that can retrieve an access token for the consumer.
+ */
+ private final DefaultOAuthProvider provider;
+
+ /**
* Instantiates a requester using OAuth. The caller must initialize the
* access token before requests can be sent.
*
@@ -29,6 +37,9 @@ public class OAuthRequester extends AbstractRequester {
public OAuthRequester(ConsumerKeySecret cks) {
// create a new application-specific OAuth consumer
consumer = new DefaultOAuthConsumer(cks.getKey(), cks.getSecret());
+ // Note: Access tokens still require PIN
+ provider = new DefaultOAuthProvider(Configuration.REQUEST_TOKEN_URL,
+ Configuration.ACCESS_TOKEN_URL, Configuration.AUTHORIZE_URL);
}
/**
@@ -44,6 +55,39 @@ public class OAuthRequester extends AbstractRequester {
consumer.setTokenWithSecret(token, secret);
}
+ /**
+ * Retrieves an URL which allows an authenticated user to retrieve a PIN for
+ * this application.
+ *
+ * @return An URL.
+ * @throws IOException if an error occurred while retrieving the URL.
+ */
+ public String getAuthURL() throws IOException {
+ String authUrl;
+ try {
+ authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
+ } catch (OAuthException ex) {
+ throw new IOException(ex);
+ }
+ return authUrl;
+ }
+
+ /**
+ * Gets access tokens from a PIN (out-of-band method). The PIN can be
+ * retrieved by visiting the URL from {@code getAuthURL()}.
+ * See https://dev.twitter.com/docs/auth/pin-based-authorization
+ *
+ * @param pin The PIN as found on the page.
+ * @throws IOException if the PIN cannot be used to retrieve access tokens.
+ */
+ public void supplyPINForTokens(String pin) throws IOException {
+ try {
+ provider.retrieveAccessToken(consumer, pin);
+ } catch (OAuthException ex) {
+ throw new IOException(ex);
+ }
+ }
+
@Override
protected void preconnect(URLConnection conn) throws IOException {
try {