Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Raysn */ //ref: http://yoshihikomuto.hatenablog.jp/entry/20110613/1307949741 package tokyo.raysn.hanaasagi.app; import java.io.*; import tokyo.raysn.hanaasagi.tools.AppConfig; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.User; import twitter4j.auth.AccessToken; import twitter4j.auth.RequestToken; import twitter4j.conf.ConfigurationBuilder; public class OAuthInfo { Twitter twitter; private static OAuthWindow oAuthWindow; private static RequestToken requestToken = null; private static AccessToken accessToken = null; private static ConfigurationBuilder cb = new ConfigurationBuilder(); public OAuthInfo() { this(null); } public OAuthInfo(OAuthWindow authinfo) { ConsumerKey ck = new ConsumerKey(); //OAuth???? oAuthWindow = authinfo; //cb.setDebugEnabled(true) // .setOAuthConsumerKey(ck.getConsumerKey(0)) // .setOAuthConsumerSecret(ck.getConsumerSecret(0)); //TwitterFactory tf = new TwitterFactory(cb.build()); TwitterFactory tf = new TwitterFactory(); twitter = tf.getInstance(); //CK/CS twitter.setOAuthConsumer(ck.getConsumerKey(0), ck.getConsumerSecret(0)); try { requestToken = twitter.getOAuthRequestToken(); } catch (TwitterException te) { te.printStackTrace(); System.exit(1); } if (oAuthWindow != null) { oAuthWindow.setOAuthUrlOnWindow(requestToken.getAuthorizationURL()); } } public AccessToken getAccessTokenWithPinCode(String pin) throws TwitterException { accessToken = twitter.getOAuthAccessToken(requestToken, pin); return accessToken; } public Twitter getTwitterInstance() { return twitter; } public static AccessToken loadAccessToken() { File f = createAccessTokenFileName(); ObjectInputStream is = null; try { is = new ObjectInputStream(new FileInputStream(f)); AccessToken accessToken = (AccessToken) is.readObject(); return accessToken; } catch (IOException e) { return null; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void storeAccessToken(AccessToken accessToken) { //???? File f = createAccessTokenFileName(); //?????,. File d = f.getParentFile(); if (!(d.exists())) { d.mkdirs(); } //????? ObjectOutputStream os = null; try { os = new ObjectOutputStream(new FileOutputStream(f)); os.writeObject(accessToken); } catch (IOException e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } // ?????? static File createAccessTokenFileName() { // System.getProperty("user.home") ??? // ? AppConfig ac = AppConfig.getSingleton(); String s = ac.getTokenDirectory() + "accessToken.dat"; return new File(s); } }