From 7c2d2b66a21b9a0889e78cf5ff0982245c47bb69 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Wed, 30 Apr 2014 23:32:01 +0200 Subject: TwitterApi: retry asking for PIN if missing Also provide access to requester for stream thingey. --- src/mining/TwitterApi.java | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/mining/TwitterApi.java b/src/mining/TwitterApi.java index 7f431c3..372aade 100644 --- a/src/mining/TwitterApi.java +++ b/src/mining/TwitterApi.java @@ -93,9 +93,22 @@ public class TwitterApi { throw new IOException("Unable to retrieve an access token"); } String authUrl = oreq.getAuthURL(); - oreq.supplyPINForTokens(ps.requestPin(authUrl)); + for (int i = 0; i < 5; ++i) { + try { + String pin = ps.requestPin(authUrl); + // stop asking if an empty PIN was supplied + if (!pin.isEmpty()) { + oreq.supplyPINForTokens(pin); + } + break; + } catch (IOException ex) { + System.err.println("Trying again, PIN rejected: " + ex); + } + } secrets = oreq.getSecrets(); - assert secrets != null : "PIN accepted, but no access tokens?"; + if (secrets == null) { + throw new IOException("Unable to get access tokens"); + } cfg.setProperty(CFG_OAUTH_TOKEN, secrets.getToken()); cfg.setProperty(CFG_OAUTH_SECRET, secrets.getSecret()); cfg.save(); @@ -130,6 +143,13 @@ public class TwitterApi { return new Builder(resource); } + /** + * @return The requester instance associated with this. + */ + public Requester getRequester() { + return requester; + } + public class Builder { private final String resource; -- cgit v1.2.1