summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-04-24 17:46:45 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-04-25 01:40:08 +0200
commit3178d4d0a92e4cfd2eff84a2a298acf514337633 (patch)
tree733b9ffb9cebf19386a2143e12f08904322d9eda
parent80ab68ef65a8ef3d13bd781fe3e927106026cfdf (diff)
downloadTwitterDataAnalytics-3178d4d0a92e4cfd2eff84a2a298acf514337633.tar.gz
OAuthRequester: allow to set access tokens
-rw-r--r--src/mining/OAuthRequester.java28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/mining/OAuthRequester.java b/src/mining/OAuthRequester.java
index f5b6a10..8421d42 100644
--- a/src/mining/OAuthRequester.java
+++ b/src/mining/OAuthRequester.java
@@ -6,6 +6,7 @@ import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthException;
import org.json.JSONObject;
+import support.ConsumerKeySecret;
/**
* An API requester that uses OAuth to sign its requests.
@@ -19,10 +20,28 @@ public class OAuthRequester extends AbstractRequester {
*/
private final OAuthConsumer consumer;
- public OAuthRequester(String consumerKey, String consumerSecret) {
+ /**
+ * Instantiates a requester using OAuth. The caller must initialize the
+ * access token before requests can be sent.
+ *
+ * @param cks The consumer secrets provided by Twitter.
+ */
+ public OAuthRequester(ConsumerKeySecret cks) {
// create a new application-specific OAuth consumer
- consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
- // TODO: access tokens?
+ consumer = new DefaultOAuthConsumer(cks.getKey(), cks.getSecret());
+ }
+
+ /**
+ * Set the access token to sign apps with. This access token can be
+ * retrieved from dev.twitter.com (see
+ * https://dev.twitter.com/docs/auth/tokens-devtwittercom) or via a PIN
+ * (https://dev.twitter.com/docs/auth/pin-based-authorization).
+ *
+ * @param token Access token.
+ * @param secret Access token secret.
+ */
+ public void setAccessToken(String token, String secret) {
+ consumer.setTokenWithSecret(token, secret);
}
@Override
@@ -36,6 +55,9 @@ public class OAuthRequester extends AbstractRequester {
@Override
public boolean isValid() throws IOException {
+ if (consumer.getToken() == null) {
+ throw new IOException("I... Need... ACCESS TOKENS!!!");
+ }
// NOTE: this actually contributes to the ratelimit (12/minute)
// TODO: find alternative that does not hit the ratelimit
JSONObject obj = getJSONRelax("1/application/rate_limit_status");