Linked Data &Semantic WebTechnology
Development ofTwitter Applications
Part 3. OAuth
Dr. Myungjin Lee
2Linked Data & Semantic Web Technology
Twitter4J
• What is Twitter4J?– an unofficial Java library for the Twitter API– easily to integrate your Java application with the Twit-
ter service– http://twitter4j.org/en/index.html
• Features– 100% Pure Java - works on any Java Platform version
5 or later– Android platform and Google App Engine ready– Zero dependency : No additional jars required– Built-in OAuth support– Out-of-the-box gzip support– 100% Twitter API 1.1 compatible
• API Support matrix– http://twitter4j.org/en/api-support.html
3Linked Data & Semantic Web Technology
OAuth
• What is OAuth?– an open standard for authorization– a method for clients to access server resources on be-
half of a resource owner– a process for end-users to authorize third-party access
to their server resources without sharing their creden-tials
• Twitter’s OAuth– OAuth v1.0a to provide authorized access to Twitter
API– using HMAC-SHA1 signature
4Linked Data & Semantic Web Technology
OAuth Process for Desktop Application
Linked Data & Semantic Web Technology
Registering Your Application
• https://dev.twitter.com/apps/new
your application nameyour application description
your application’s publicly accessible home page
webpage URL where twitter returns after successfully authenticating
6Linked Data & Semantic Web Technology
Registering Your Application
Consumer Key and Secret to get user’s Access Token
7Linked Data & Semantic Web Technology
Application type
• Read only– Read Tweets from your timeline.– See who you follow.
• Read and Write– Read Tweets from your timeline.– See who you follow, and follow new people.– Update your profile.– Post Tweets for you.
• Read, Write and Access direct messages– Read Tweets from your timeline.– See who you follow, and follow new people.– Update your profile.– Post Tweets for you.– Access your direct messages.
8Linked Data & Semantic Web Technology
Primary Classes and Interfaces of Twit-ter4J
• TwitterFactory Class– A factory class for Twitter
• Twitter Interface– basic interface to use all of APIs
• AccessToken Class– Representing authorized Access Token which is
passed to the service provider in order to access pro-tected resources
9Linked Data & Semantic Web Technology
Getting Access Token1. // The factory instance is re-useable and thread safe.2. Twitter twitter = TwitterFactory.getSingleton();3. twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");4. RequestToken requestToken = null;5. try {6. requestToken = twitter.getOAuthRequestToken();7. } catch (TwitterException e) {8. // TODO Auto-generated catch block9. e.printStackTrace();10. }11. AccessToken accessToken = null;12. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));13. while (null == accessToken) {14. System.out.println("Open the following URL and grant access to your ac-
count:");15. System.out.println(requestToken.getAuthorizationURL());16. System.out.print("Enter the PIN(if aviailable) or just hit enter.
[PIN]:");17. String pin = new String();18. try {19. pin = br.readLine();20. } catch (IOException e) {21. // TODO Auto-generated catch block22. e.printStackTrace();23. }24. try {25. if (pin.length() > 0) {26. accessToken =
twitter.getOAuthAccessToken(requestToken, pin);27. } else {28. accessToken = twitter.getOAuthAccessToken();29. }30. } catch (TwitterException te) {31. if (401 == te.getStatusCode()) {32. System.out.println("Unable to get the access
token.");33. } else {34. te.printStackTrace();35. }36. }37. }38. // persist to the accessToken for future reference.39. try {40. storeAccessToken(twitter.verifyCredentials().getId(), accessToken);41. } catch (TwitterException e) {42. // TODO Auto-generated catch block43. e.printStackTrace();44. }
10Linked Data & Semantic Web Technology
Getting Access Token1. private static void storeAccessToken(long useId, AccessToken accessToken) {2. // store accessToken.getToken() and accessToken.getTokenSecret()3. System.out.println("User ID: " + useId);4. System.out.println("Access Token: " + accessToken.getToken());5. System.out.println("Access Token Secret: "6. + accessToken.getTokenSe-
cret());7. }
8. public static AccessToken loadAccessToken() {9. String token = "[access token]";10. String tokenSecret = "[access token secret]";11. return new AccessToken(token, tokenSecret);12. }
11Linked Data & Semantic Web Technology
Test Code for Authentication1. import twitter4j.IDs;2. import twitter4j.Paging;3. import twitter4j.Twitter;4. import twitter4j.TwitterException;5. import twitter4j.TwitterFactory;6. import twitter4j.User;7. import twitter4j.auth.AccessToken;
8. public class TwitterFriends {9. public static void main(String args[]) {10. Twitter twitter = TwitterFactory.getSingleton();11. AccessToken accessToken = TwitterAccessToken.loadAc-
cessToken();12.
twitter.setOAuthConsumer(TwitterAccessToken.consumerKey,13. TwitterAccessToken.consumer-
Secret);14. twitter.setOAuthAccessToken(accessToken);15. IDs ids = null;16. try {17. ids = twitter.getFollowersIDs(-1);18. } catch (TwitterException e) {19. // TODO Auto-generated catch block20. e.printStackTrace();21. }22. long[] idArray = ids.getIDs();23. for (int i = 0; i < idArray.length; i++) {24. User u = null;25. try {26. u =
twitter.showUser(idArray[i]);27. } catch (TwitterException e) {28. // TODO Auto-generated catch
block29. e.printStackTrace();30. }31. System.out.println("Name: " +
u.getName());32. }33. }34. }
12Linked Data & Semantic Web Technology
References• http://en.wikipedia.org/wiki/Oauth• http://twitter4j.org/en/index.html• http://www.semantics.kr/?p=185