Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Created February 10, 2011 08:34
Show Gist options
  • Save shin1ogawa/820140 to your computer and use it in GitHub Desktop.
Save shin1ogawa/820140 to your computer and use it in GitHub Desktop.
package com.shin1ogawa;
package com.shin1ogawa;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import org.junit.Test;
public class OAuthGetTokenTest {
static final String SERVER = "*****.appspot.com";
static final String REQUEST_TOKEN_ENDPOINT_URL = "https://" + SERVER
+ "/_ah/OAuthGetRequestToken";
static final String AUTHORIZE_WEBSITE_URL = "https://" + SERVER
+ "/_ah/OAuthAuthorizeToken";
static final String ACCESS_TOKEN_ENDPOINT_URL = "https://" + SERVER
+ "/_ah/OAuthGetAccessToken";
static final String CONSUMER_KEY = "anonymous";
static final String CONSUMER_SECRET = "anonymous";
static final String TESTURL_GET = "https://" + SERVER + "/admin/importer/reverseGeocode";
static final String TESTURL_POST = "https://" + SERVER + "/hoge";
@Test
public void getToken() throws OAuthMessageSignerException,
OAuthNotAuthorizedException, OAuthExpectationFailedException,
OAuthCommunicationException, IOException, InvalidKeyException,
NoSuchAlgorithmException {
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
OAuthProvider provider = new DefaultOAuthProvider(
REQUEST_TOKEN_ENDPOINT_URL, ACCESS_TOKEN_ENDPOINT_URL,
AUTHORIZE_WEBSITE_URL);
String url = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
System.out.println(url);
System.out
.println("open above url and grant, then input versificationCode: ");
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
String verificationCode = reader.readLine().trim();
System.out.println("using versificationCode:\"" + verificationCode
+ "\"");
provider.retrieveAccessToken(consumer, verificationCode);
System.out.println("consumer.getToken()=" + consumer.getToken());
System.out.println("consumer.getTokenSecret()="
+ consumer.getTokenSecret());
System.out.println(provider.getResponseParameters());
// get(TESTURL_GET, consumer);
post(TESTURL_POST,
"application/json",
"{\"name\":\"taskHoge\", \"tags\":[\"tagZ\"]}"
.getBytes("utf-8"), consumer.getToken(),
consumer.getTokenSecret());
}
void post(String url, String contentType, byte[] bytes, String token,
String secret) throws InvalidKeyException,
NoSuchAlgorithmException, IOException {
Map<String, String> map = new HashMap<String, String>();
String base64String = OAuthUtil.byteArrayToBase64String(bytes);
System.out.println("-----base64");
System.out.println(base64String);
map.put("oauth_body_hash", base64String);
String authorizeValue = OAuthUtil.getAuthorizationHeaderValue("POST",
url, map, CONSUMER_KEY, CONSUMER_SECRET, token, secret);
System.out.println("-----authorizeValue");
System.out.println(authorizeValue);
URL _url = new URL(url);
HttpURLConnection connection = (HttpURLConnection) _url
.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Content-Length",
String.valueOf(bytes.length));
connection.setRequestProperty("Authorization", authorizeValue);
connection.getOutputStream().write(bytes);
connection.getOutputStream().flush();
connection.connect();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
System.out.println("-----response");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println(connection.getResponseCode());
System.out.println(connection.getResponseMessage());
throw e;
}
}
static void get(String url, OAuthConsumer consumer) throws IOException,
OAuthMessageSignerException, OAuthExpectationFailedException,
OAuthCommunicationException {
URL _url = new URL(url);
HttpURLConnection connection = (HttpURLConnection) _url
.openConnection();
HttpURLConnection.setFollowRedirects(false);
consumer.sign(connection);
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment