Created
July 25, 2012 18:38
-
-
Save Chiggins/3177793 to your computer and use it in GitHub Desktop.
Testing out Neoseekers OAuth API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.StringWriter; | |
import oauth.signpost.OAuth; | |
import oauth.signpost.OAuthConsumer; | |
import oauth.signpost.OAuthProvider; | |
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; | |
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider; | |
import oauth.signpost.exception.OAuthCommunicationException; | |
import oauth.signpost.exception.OAuthExpectationFailedException; | |
import oauth.signpost.exception.OAuthMessageSignerException; | |
import oauth.signpost.exception.OAuthNotAuthorizedException; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
public class NeoseekerOAuthTest { | |
private static String consumerKey = "64e008663d61ea899f90a5e05e16fc"; | |
private static String consumerSecret = "ec1007fc62"; | |
private static String requestTokenURL = "http://api.neoseeker.com/oauth/request.php"; | |
private static String accessTokenURL = "http://api.neoseeker.com/oauth/access.php"; | |
private static String authTokenURL = "http://api.neoseeker.com/oauth/authorize.php"; | |
public static void main(String[] args) { | |
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); | |
OAuthProvider provider = new CommonsHttpOAuthProvider(requestTokenURL, accessTokenURL, authTokenURL); | |
System.out.println("Fetching request token..."); | |
String authUrl = ""; | |
try { | |
authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND); | |
} catch (OAuthMessageSignerException | OAuthNotAuthorizedException | |
| OAuthExpectationFailedException | |
| OAuthCommunicationException e1) { | |
e1.printStackTrace(); | |
System.exit(0); | |
} | |
System.out.println("Request token: " + consumer.getToken()); | |
System.out.println("Token secret: " + consumer.getTokenSecret()); | |
System.out.println("Now visit:\n" + authUrl + "\n... and grant this app authorization"); | |
System.out.println("Enter the verification code and hit ENTER when you're done:"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
String verificationCode = ""; | |
try { | |
verificationCode = br.readLine(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
System.out.println("Fetching access token..."); | |
try { | |
provider.retrieveAccessToken(consumer, verificationCode.trim()); | |
} catch (OAuthMessageSignerException | OAuthNotAuthorizedException | |
| OAuthExpectationFailedException | OAuthCommunicationException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
System.out.println("Access token: " + consumer.getToken()); | |
System.out.println("Token secret: " + consumer.getTokenSecret()); | |
HttpGet request = new HttpGet("http://api.neoseeker.com/members/me/"); | |
try { | |
consumer.sign(request); | |
} catch (OAuthMessageSignerException | OAuthExpectationFailedException | |
| OAuthCommunicationException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
System.out.println("Sending request..."); | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpResponse response = null; | |
try { | |
response = httpClient.execute(request); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
InputStream is = null; | |
try { | |
is = response.getEntity().getContent(); | |
} catch (IllegalStateException | IOException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
StringWriter writer = new StringWriter(); | |
try { | |
IOUtils.copy(is, writer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.exit(0); | |
} | |
String result = writer.toString(); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment