Download presentation
Presentation is loading. Please wait.
Published byArild Lorentzen Modified over 5 years ago
1
Development of Twitter Applications Part 3. OAuth
Dr. Myungjin Lee
2
Twitter4J What is Twitter4J? Features API Support matrix
an unofficial Java library for the Twitter API easily to integrate your Java application with the Twitter service 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
3
OAuth What is OAuth? Twitter’s 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
4
OAuth Process for Desktop Application
5
Registering Your Application
your application name your application description your application’s publicly accessible home page webpage URL where twitter returns after successfully authenticating
6
Registering Your Application
Consumer Key and Secret to get user’s Access Token
7
Application type Read only Read and Write
Read Tweets from your timeline. See who you follow. Read and Write See who you follow, and follow new people. Update your profile. Post Tweets for you. Read, Write and Access direct messages Access your direct messages.
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
9
Getting Access Token // The factory instance is re-useable and thread safe. Twitter twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer("[consumer key]", "[consumer secret]"); RequestToken requestToken = null; try { requestToken = twitter.getOAuthRequestToken(); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } AccessToken accessToken = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (null == accessToken) { System.out.println("Open the following URL and grant access to your account:"); System.out.println(requestToken.getAuthorizationURL()); System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); String pin = new String(); pin = br.readLine(); } catch (IOException e) { if (pin.length() > 0) { accessToken = twitter.getOAuthAccessToken(requestToken, pin); } else { accessToken = twitter.getOAuthAccessToken(); } catch (TwitterException te) { if (401 == te.getStatusCode()) { System.out.println("Unable to get the access token."); te.printStackTrace(); // persist to the accessToken for future reference. storeAccessToken(twitter.verifyCredentials().getId(), accessToken);
10
Getting Access Token private static void storeAccessToken(long useId, AccessToken accessToken) { // store accessToken.getToken() and accessToken.getTokenSecret() System.out.println("User ID: " + useId); System.out.println("Access Token: " + accessToken.getToken()); System.out.println("Access Token Secret: " + accessToken.getTokenSecret()); } public static AccessToken loadAccessToken() { String token = "[access token]"; String tokenSecret = "[access token secret]"; return new AccessToken(token, tokenSecret);
11
Test Code for Authentication
import twitter4j.IDs; import twitter4j.Paging; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.User; import twitter4j.auth.AccessToken; public class TwitterFriends { public static void main(String args[]) { Twitter twitter = TwitterFactory.getSingleton(); AccessToken accessToken = TwitterAccessToken.loadAccessToken(); twitter.setOAuthConsumer(TwitterAccessToken.consumerKey, TwitterAccessToken.consumerSecret); twitter.setOAuthAccessToken(accessToken); IDs ids = null; try { ids = twitter.getFollowersIDs(-1); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } long[] idArray = ids.getIDs(); for (int i = 0; i < idArray.length; i++) { User u = null; u = twitter.showUser(idArray[i]); System.out.println("Name: " + u.getName());
12
References http://en.wikipedia.org/wiki/Oauth
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.