Nowadays every project need social registration for attract more users but according to most developers it is a hard task. OAuth/OAuth2 was difficult? Configuring ScribeJava is so easy your grandma can do it!
Supports all major OAuth APIs out-of-the-box
- AWeber (http://www.aweber.com/)
- Box (https://www.box.com/)
- Digg (http://digg.com/)
- Доктор на работе (https://www.doktornarabote.ru/)
- Facebook (https://www.facebook.com/)
- Flickr (https://www.flickr.com/)
- Foursquare (https://foursquare.com/)
- Freelancer (https://www.freelancer.com/)
- Genius (http://genius.com/)
- GitHub (https://github.com/)
- Google (https://www.google.com/)
- HeadHunter ХэдХантер (https://hh.ru/)
- Imgur (http://imgur.com/)
- Kaixin 开心网 (http://www.kaixin001.com/)
- LinkedIn (https://www.linkedin.com/)
- Microsoft Live (https://login.live.com/)
- Mail.Ru (https://mail.ru/)
- Meetup (http://www.meetup.com/)
- NAVER (http://www.naver.com/)
- NetEase (http://www.163.com/)
- Odnoklassniki Одноклассники (http://ok.ru/)
- Pinterest (https://www.pinterest.com/)
- 500px (https://500px.com/)
- Renren (http://renren.com/)
- Salesforce (https://www.salesforce.com/)
- Sina (http://www.sina.com.cn/ http://weibo.com/login.php)
- Skyrock (http://skyrock.com/)
- sohu 搜狐 (http://www.sohu.com/)
- StackExchange (http://stackexchange.com/)
- The Things Network (v1-staging and v2-preview) (https://www.thethingsnetwork.org/)
- Trello (https://trello.com/)
- Tumblr (https://www.tumblr.com/)
- TUT.BY (http://www.tut.by/)
- Twitter (https://twitter.com/)
- Viadeo (http://viadeo.com/)
- VK ВКонтакте (http://vk.com/)
- XING (https://www.xing.com/)
- Yahoo (https://www.yahoo.com/)
- Misfit (http://misfit.com/)
In this tutorial we will walk through an example on how to use ScribeJava with Twitter (OAuth 1.0a).
Install ScribeJava
First of all, you need to install ScribeJava. You either download two jars manually from the downloads page and http://mvnrepository.com/artifact/com.github.scribejava/scribejava-core and include apache commons codec, or just let maven take care of everything adding this to your pom.xml file:
<dependency> <groupId>com.github.scribejava</groupId> <artifactId>scribejava-apis</artifactId> <version>3.2.0</version> // please use always the latest version </dependency>
Or for gradle:
dependencies { compile 'com.github.scribejava:scribejava-apis:3.2.0' }
Create the OAuthService object
final OAuth10aService service = new ServiceBuilder() .apiKey("your_api_key") .apiSecret("your_api_secret") .build(TwitterApi.instance());
Yup, that’s it. 🙂
The example uses OOB OAuth, if you want to pass a callbackUrl so that Twitter redirects you there just add
a callback("http://your_callback_url")
call before build()
Get the request token
final OAuth1RequestToken requestToken = service.getRequestToken();
Easy right?
Making the user validate your request token
Let’s help your users authorize your app to do the OAuth calls.
For this you need to redirect them to the following URL:
String authUrl = service.getAuthorizationUrl(requestToken);
After this either the user will get a verifier code (if this is an OOB request) or you’ll receive a redirect from Twitter with the verifier and the requestToken on it (if you provided a callbackUrl)
Get the access Token
Now that you have (somehow) the verifier, you need to exchange your requestToken and verifier for an accessToken which is the one used to sign requests.
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, "verifier you got from the user/callback");
Sign request
You are all set to make your first API call, so let’s do it!
final OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json", service); service.signRequest(accessToken, request); // the access token from step 4 final Response response = request.send(); System.out.println(response.getBody());
That’s it! You already know everything you need to start building a cool OAuth application.
Good Luck!
There is definately a great deal to find out about this issue.
I like all of the points you’ve made.
Spot on with this write-up, I seriously think this site needs much more attention.
I’ll probably be back again to read more, thanks
for the advice!
Thank you for any other great post. Where else may anyone get that type of info in such a perfect means of writing?
I have a presentation next week, and I am at the search for such info.