現在Twitterクライアントを、Twitter4jとFabricで開発しています。
参考にしているサイトはAndroid再入門- Twitterクライアントを作ってみよう - という記事です。
OAuth認証とツイート機能はFabricを用いて成功したのですが、ユーザー情報の取得やタイムラインを表示するところでつまずいています。
記事ではTwitter4jを用いてアクセストークンを取得し、タイムラインを表示するために用いているようですが、Fabricを用いてログインした場合、どのようにアクセストークンを取得するのかがわかりません。
方法など、ご教授願います。
java
1public class TwitterUtils { 2 private static final String TOKEN = "token"; 3 private static final String TOKEN_SECRET = "token_secret"; 4 private static final String PREF_NAME = "twitter_access_token"; 5 6 public static Twitter getTwitterInstance(Context context) { 7 String consumerKey = context.getString(R.string.twitter_consumer_key); 8 String consumerSecret = context.getString(R.string.twitter_consumer_secret); 9 10 TwitterFactory factory = new TwitterFactory(); 11 Twitter twitter = factory.getInstance(); 12 twitter.setOAuthConsumer(consumerKey, consumerSecret); 13 14 if (hasAccessToken(context)) { 15 twitter.setOAuthAccessToken(loadAccessToken(context)); 16 } 17 return twitter; 18 } 19 20 21 public static void storeAccessToken(Context context, AccessToken accessToken) { 22 SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, 23 Context.MODE_PRIVATE); 24 SharedPreferences.Editor editor = preferences.edit(); 25 editor.putString(TOKEN, accessToken.getToken()); 26 editor.putString(TOKEN_SECRET, accessToken.getTokenSecret()); 27 editor.commit(); 28 } 29 30 public static AccessToken loadAccessToken(Context context) { 31 SharedPreferences preferences = context.getSharedPreferences(PREF_NAME, 32 Context.MODE_PRIVATE); 33 String token = preferences.getString(TOKEN, null); 34 String tokenSecret = preferences.getString(TOKEN_SECRET, null); 35 if (token != null && tokenSecret != null) { 36 return new AccessToken(token, tokenSecret); 37 } else { 38 return null; 39 } 40 } 41 42 public static boolean hasAccessToken(Context context) { 43 return loadAccessToken(context) != null; 44 } 45 46} 47
java
1public class LoginWithTwitterActivity extends Activity { 2 private TwitterLoginButton loginButton; 3 private static final String TWITTER_KEY=""; 4 private static final String TWITTER_SECRET = ""; 5 6 @Override 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 final TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); 10 Fabric.with(this, new Twitter(authConfig)); 11 setContentView(R.layout.activity_login); 12 13 loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button); 14 loginButton.setCallback(new Callback<TwitterSession>() { 15 @Override 16 public void success(Result<TwitterSession> result) { 17 // The TwitterSession is also available through: 18 // Twitter.getInstance().core.getSessionManager().getActiveSession() 19 TwitterSession session = result.data; 20 // TODO: Remove toast and use the TwitterSession's userID 21 // with your app's user model 22 String msg = "@" + session.getUserName() + "でログインしました!"; 23 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 24 goHome(); 25 } 26 27 @Override 28 public void failure(TwitterException exception) { 29 Log.d("TwitterKit", "Login with Twitter failure", exception); 30 } 31 }); 32 33 } 34 35 @Override 36 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 37 super.onActivityResult(requestCode, resultCode, data); 38 // Make sure that the loginButton hears the result from any 39 // Activity that it triggered. 40 loginButton.onActivityResult(requestCode, resultCode, data); 41 } 42 43 private void goHome(){ 44 final Intent intent=new Intent(LoginWithTwitterActivity.this,PostTweet.class); 45 startActivity(intent); 46 } 47}
java
1public class Home extends ListActivity{ 2 3 TweetAdapter adapter; 4 Twitter twitter; 5 6 @Override 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 10 /* 11 *アクセストークンが無い場合、認証用アクティビティ起動 12 */ 13 if(!TwitterUtils.hasAccessToken(this)){ 14 Intent intent=new Intent(Home.this,LoginWithTwitterActivity.class); 15 startActivity(intent); 16 finish(); 17 } else{ 18 adapter = new TweetAdapter(this); 19 setListAdapter(adapter); 20 21 twitter = TwitterUtils.getTwitterInstance(this); 22 reloadTimeLine(); 23 24 } 25 } 26 27 public class TweetAdapter extends ArrayAdapter<twitter4j.Status>{ 28 private LayoutInflater inflater; 29 public TweetAdapter(Context context){ 30 super(context,R.layout.activity_home); 31 inflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 32 } 33 34 @Override 35 public View getView(int position, View convertView, ViewGroup parent) { 36 Status item=getItem(position); 37 TextView name=(TextView)convertView.findViewById(R.id.account_name); 38 name.setText(item.getUser().getName()); 39 TextView screenName = (TextView) convertView.findViewById(R.id.user_name); 40 screenName.setText("@" + item.getUser().getScreenName()); 41 TextView text = (TextView) convertView.findViewById(R.id.text); 42 text.setText(item.getText()); 43 return convertView; 44 } 45 } 46 47 private void reloadTimeLine() { 48 AsyncTask<Void, Void, List<Status>> task = new AsyncTask<Void, Void, List<twitter4j.Status>>() { 49 @Override 50 protected List<twitter4j.Status> doInBackground(Void... params) { 51 try { 52 return twitter.getHomeTimeline(); 53 } catch (twitter4j.TwitterException e) { 54 e.printStackTrace(); 55 } 56 return null; 57 } 58 59 @Override 60 protected void onPostExecute(List<twitter4j.Status> result) { 61 if (result != null) { 62 adapter.clear(); 63 for (twitter4j.Status status : result) { 64 adapter.add(status); 65 } 66 getListView().setSelection(0); 67 } else { 68 showToast("タイムラインの取得に失敗しました。。。"); 69 } 70 } 71 }; 72 task.execute(); 73 } 74 75 private void showToast(String text) { 76 Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 77 } 78}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。