Development of Twitter Application #3 - OAuth
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Development of Twitter Application #3 - OAuth

  • 1,637 views
Uploaded on

This series of slides describes how to develop a twitter application. ...

This series of slides describes how to develop a twitter application.
This slide contains OAuth and its programming with Twitter4J.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,637
On Slideshare
1,637
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
17
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Linked Data &Semantic WebTechnology Development ofTwitter Applications Part 3. OAuth Dr. Myungjin Lee
  • 2. Twitter4J • What is Twitter4J? – an unofficial Java library for the Twitter API – easily to integrate your Java application with the Twitter 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 2Linked Data & Semantic Web Technology
  • 3. OAuth • What is OAuth? – an open standard for authorization – a method for clients to access server resources on behalf of a resource owner – a process for end-users to authorize third-party access to their server resources without sharing their credentials • Twitter’s OAuth – OAuth v1.0a to provide authorized access to Twitter API – using HMAC-SHA1 signature 3Linked Data & Semantic Web Technology
  • 4. OAuth Process for Desktop Application 4Linked Data & Semantic Web Technology
  • 5. Registering Your Application • https://dev.twitter.com/apps/new your application name your application description your application’s publicly accessible home page webpage URL where twitter returns after successfully authenticatingLinked Data & Semantic Web Technology
  • 6. Registering Your Application Consumer Key and Secret to get user’s Access Token 6Linked Data & Semantic Web Technology
  • 7. 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. 7Linked Data & Semantic Web Technology
  • 8. Primary Classes and Interfaces of Twitter4J • 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 protected resources 8Linked Data & Semantic Web Technology
  • 9. Getting Access Token 1. // 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 block 9. 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 account:"); 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 block 22. 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 block 43. e.printStackTrace(); 44. } 9Linked Data & Semantic Web Technology
  • 10. Getting Access Token 1. 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.getTokenSecret()); 7. } 8. public static AccessToken loadAccessToken() { 9. String token = "[access token]"; 10. String tokenSecret = "[access token secret]"; 11. return new AccessToken(token, tokenSecret); 12. } 10Linked Data & Semantic Web Technology
  • 11. Test Code for Authentication 1. 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.loadAccessToken(); 12. twitter.setOAuthConsumer(TwitterAccessToken.consumerKey, 13. TwitterAccessToken.consumerSecret); 14. twitter.setOAuthAccessToken(accessToken); 15. IDs ids = null; 16. try { 17. ids = twitter.getFollowersIDs(-1); 18. } catch (TwitterException e) { 19. // TODO Auto-generated catch block 20. 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 block 29. e.printStackTrace(); 30. } 31. System.out.println("Name: " + u.getName()); 32. } 33. } 34. } 11Linked Data & Semantic Web Technology
  • 12. References • http://en.wikipedia.org/wiki/Oauth • http://twitter4j.org/en/index.html • http://www.semantics.kr/?p=185 12Linked Data & Semantic Web Technology