blob: f5b6a1034a13ca0e05f9105608f97087b551b110 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package mining;
import java.io.IOException;
import java.net.URLConnection;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthException;
import org.json.JSONObject;
/**
* An API requester that uses OAuth to sign its requests.
*
* @author Peter Wu
*/
public class OAuthRequester extends AbstractRequester {
/**
* Instance that signs signs HTTP requests with an OAuth token and secret.
*/
private final OAuthConsumer consumer;
public OAuthRequester(String consumerKey, String consumerSecret) {
// create a new application-specific OAuth consumer
consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret);
// TODO: access tokens?
}
@Override
protected void preconnect(URLConnection conn) throws IOException {
try {
consumer.sign(conn);
} catch (OAuthException ex) {
throw new IOException(ex);
}
}
@Override
public boolean isValid() throws IOException {
// 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");
return !obj.has("errors");
}
}
|