Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package doublespark;
import static spark.Spark.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
// http://www.sparkjava.com/
import spark.*;
// http://code.google.com/p/oauth-signpost/
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
// http://code.google.com/p/google-gson/
import com.google.gson.Gson;
public class Sample {
static String CONSUMER_KEY = "YOUR_APP_CONSUMER_KEY";
static String CONSUMER_SECRET = "YOUR_APP_CONSUMER_SECRET";
static String REQUEST_TOKEN_URL = "https://tasks.7kai.org/oauth/request_token";
static String AUTHORIZE_URL = "https://tasks.7kai.org/oauth/authorize";
static String ACCESS_TOKEN_URL = "https://tasks.7kai.org/oauth/access_token";
static String CALLBACK_URL = "http://127.0.0.1:4567/callback";
static public OAuthConsumer getConsumer() {
return new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
}
static public OAuthProvider getProvider() {
OAuthProvider provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL, AUTHORIZE_URL);
return provider;
}
public static void main(String[] args) {
get(new Route("/") {
@Override
public Object handle(Request request, Response response) {
OAuthConsumer consumer = getConsumer();
OAuthProvider provider = getProvider();
try {
String authUrl = provider.retrieveRequestToken(consumer,
CALLBACK_URL);
// redirectより先にセットしないとダメ
request.session(true);
request.session().attribute("request_token",
consumer.getToken());
request.session().attribute("request_token_secret",
consumer.getTokenSecret());
response.redirect(authUrl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
});
get(new Route("/callback") {
@Override
public Object handle(Request request, Response response) {
OAuthConsumer consumer = getConsumer();
OAuthProvider provider = getProvider();
Session session = request.session();
consumer.setTokenWithSecret(
session.attribute("request_token").toString(),
session.attribute("request_token_secret").toString());
try {
// 指定しないと oauth_verifier が飛ばない
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer,
request.queryParams("oauth_verifier").toString());
session.attribute("access_token", consumer.getToken());
session.attribute("access_token_secret", consumer.getTokenSecret());
response.redirect("/me");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
});
get(new Route("/me") {
@Override
public Object handle(Request request, Response response) {
OAuthConsumer consumer = getConsumer();
Session session = request.session();
consumer.setTokenWithSecret(
session.attribute("access_token").toString(),
session.attribute("access_token_secret").toString());
String body = "";
try {
URL url = new URL("https://tasks.7kai.org/api/1/account/me");
HttpURLConnection api_request = (HttpURLConnection) url
.openConnection();
consumer.sign(api_request);
api_request.connect();
InputStream in = api_request.getInputStream();
BufferedReader r = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String line = null;
while ((line = r.readLine()) != null) {
body = body + line;
}
Gson gson = new Gson();
Object natural = gson.fromJson(body, Object.class);
System.out.println(natural);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return body;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment