My apologies for the extremely late support and code fix. I have been working on my employer's new project. Seeing that they want to include OAuth on their application, I thought it was a good time to update this library. Hopefully this will be my final JOAuth release (as I've decided to move forward to creating an OAuth framework).
JOAuth 1.3.1 is a release to conform to RFC 5849 strict. This allows OAuth to conform to Strict OAuth Service Providers, such as LinkedIn (which conforms to OAuth1a specification).
The code has been simplified, fixed and included support for HTTP parameters with multiple values (on a key).
I have demonstrated (below) a simple example on how OAuth 1 can be done (using Twitter, of course, as well as Facebook) to retrieve authorization tokens. The former uses OAuth 1 (lenient), while the latter uses OAuth 2 (draft 0). It's up to the developer to understand the OAuth authorization flow.
I hope that you enjoy using OAuth library as much as I had fun working on the fix. JOAuth still has the service to receive authorization tokens (just look at the joauth tag on StackOverflow on how to apply it on yours).
If you have used the previous version of JOAuth, please update your code errors, as some methods on the consumer (particularly OAuth1Consumer) has changed. Oh, did I mention that I Mavenized JOAuth? :-)
Download here.
Enjoy!
LinkedIn Example:
Facebook Example:
JOAuth 1.3.1 is a release to conform to RFC 5849 strict. This allows OAuth to conform to Strict OAuth Service Providers, such as LinkedIn (which conforms to OAuth1a specification).
The code has been simplified, fixed and included support for HTTP parameters with multiple values (on a key).
I have demonstrated (below) a simple example on how OAuth 1 can be done (using Twitter, of course, as well as Facebook) to retrieve authorization tokens. The former uses OAuth 1 (lenient), while the latter uses OAuth 2 (draft 0). It's up to the developer to understand the OAuth authorization flow.
I hope that you enjoy using OAuth library as much as I had fun working on the fix. JOAuth still has the service to receive authorization tokens (just look at the joauth tag on StackOverflow on how to apply it on yours).
If you have used the previous version of JOAuth, please update your code errors, as some methods on the consumer (particularly OAuth1Consumer) has changed. Oh, did I mention that I Mavenized JOAuth? :-)
Download here.
Enjoy!
LinkedIn Example:
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.io.IOException; import net.oauth.browser.BrowserManager; import net.oauth.consumer.OAuth1Consumer; import net.oauth.exception.OAuthException; import net.oauth.http.impl.DefaultHttpClient; import net.oauth.provider.OAuth1ServiceProvider; import net.oauth.signature.impl.OAuthHmacSha1Signature; import net.oauth.token.oauth1.AccessToken; import net.oauth.token.oauth1.AuthorizedToken; import net.oauth.token.oauth1.RequestToken; /** * @author Buhake Sindi * @since 14 June 2011 * */ public class LinkedInExample { private static final String API_KEY = "SOME_KEY" ; private static final String API_SECRET = "SOME_SECRET" ; // private static final String CALLBACK_URL = "http://localhost:8080/myapp/oauth/request_token_ready"; private static final String CALLBACK_URL = "oob" ; private OAuth1Consumer consumer; /** * */ public LinkedInExample() { super (); // TODO Auto-generated constructor stub consumer = new OAuth1Consumer(API_KEY, API_SECRET, new OAuth1ServiceProvider(LINKEDIN_API_URL + "/uas/oauth/requestToken" , LINKEDIN_API_URL + "/uas/oauth/authorize" , LINKEDIN_API_URL + "/uas/oauth/accessToken" )); consumer.setClient( new DefaultHttpClient()); } public RequestToken requestUnauthorizedRequestToken() throws OAuthException { return consumer.requestUnauthorizedToken(LINKEDIN_API_URL, CALLBACK_URL, null , new OAuthHmacSha1Signature()); } public String getAuthorizationUrl(RequestToken token) throws OAuthException { return consumer.createOAuthUserAuthorizationUrl(token, null ); } public AccessToken requestAccessToken(RequestToken requestToken, String verifier) throws OAuthException { return consumer.requestAccessToken(LINKEDIN_API_URL, requestToken, verifier, new OAuthHmacSha1Signature()); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { LinkedInExample example = new LinkedInExample(); RequestToken rt = example.requestUnauthorizedRequestToken(); //Now that we have request token, let's authorize it.... String url = example.getAuthorizationUrl(rt); System.out.println(url); BrowserManager.getInstance().getBrowser().browse(url); //Getting access token, don't forget, our oauth_callback was "oob", so get PIN from LinkedIn //PIN is the verifier here... AccessToken accessToken = example.requestAccessToken(rt, "someverifier" ); System.out.println(accessToken.getToken()); } catch (OAuthException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
Facebook Example:
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import java.io.IOException; import net.oauth.browser.Browser; import net.oauth.browser.BrowserManager; import net.oauth.consumer.OAuth2Consumer; import net.oauth.enums.GrantType; import net.oauth.enums.ResponseType; import net.oauth.exception.OAuthException; import net.oauth.parameters.OAuth2Parameters; import net.oauth.token.oauth2.AccessToken; import com.neurologic.oauth.providers.FacebookServiceProvider; /** * @author Buhake Sindi * @since 01 March 2012 * */ public class FacebookExample { private static final String API_KEY = "API_KEY" ; private static final String API_SECRET = "API_SECRET" ; /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { //Create an OAuth 2 consumer OAuth2Consumer consumer = new OAuth2Consumer(API_KEY, API_SECRET, new FacebookServiceProvider()); //Generate Authorization URL String authorizationUrl = consumer.generateRequestAuthorizationUrl(ResponseType.CODE, REDIRECT_URI); //Open our system browser and login to Facebook. Browser browser = BrowserManager.getInstance().getBrowser(); //Browse browser.browse(authorizationUrl); //Now, let's get access token. //Grant type must be of AUTHORIZATION_CODE //Specify the code provided by Facebook (after logging in), as well as a redirect URI. OAuth2Parameters parameters = new OAuth2Parameters(); parameters.setCode( "" ); parameters.setRedirectUri(REDIRECT_URI); AccessToken accessToken = consumer.requestAcessToken(GrantType.AUTHORIZATION_CODE, parameters); System.out.println(accessToken.getAccessToken()); } catch (OAuthException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
> Hopefully this will be my final JOAuth release
ReplyDelete> (as I've decided to move forward to creating a
> new OAuth framework).
Just when I've decided to use JOAuth in my application... no, thank you! I need stable library code that is supported and developed.
You will realize that many OAuth Java libraries are already stable and not developed. JOAuth is constantly supported (as I use it myself). OAuth 2 will only be supported as the specification keeps changing. I have decided to create an OAuth framework as there are developers who want to add their own functionality to the OAuth protocol, i.e. implement their own nonce generator, and token strings, etc.
Delete