Help us understand the problem. What is going on with this article?

大体2時間でTwitter見れるなにかを作る@Java

More than 5 years have passed since last update.

勉強会概要

前提

  • Windows8.1
  • Eclipse
  • githubの場所はこちら
  • 理解不足な点多々あり.勉強して今後追記して参ります!

まずはじめに

  1. gitのビューを開いてクローン
  2. URIの複製から上記URIにアクセス
  3. プロジェクトを握って一旦ソース系確認

TestAppを動かす

  1. SampleApplication.javaを開いてコンテキストメニュー→実行  (Javaアプリケーションで実行) わりと下の方にあります
  2. 一旦ローカルホストで確認(localhost:8080)
  3. 画面は"hello,world"だけ表示されてるはずです

画面っぽい何かを見てみる

  1. localhost:8080の後ろに "/sample"でhtmlファイルを閲覧
  2. フォームにinputすると画面に反映されます

なんでか?

  • timeleafを入れてタグにthをほうり込むと文字列が置き換わる
  • ${name}はcontroller側でmodel.attribute()としてnameの値をほうり込んでいく
  • modelはtimeleafに渡す値をマッピングしてる
  • ハンドラはspringboot側でよしなに

twitterAPIへ

  1. twiiterAPIへアクセス
  2. キーコピペしてtwitter4jpropatiesにいれる

認証系

  1. 新しくクラスを定義.こんな感じ.
TwitterAuthController.java
package jp.co.bizreach.spring_boot_sample;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationContext;
@Controller
@RequestMapping("")
public class TwitterAuthController {
    @Autowired
    private HttpSession session;
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("requestToken")
    public String requestToken(Model model) throws TwitterException{
        Configuration conf = ConfigurationContext.getInstance();
        OAuthAuthorization oauth = new OAuthAuthorization(conf);
        String callbackURL = "http://localhost:8080/accessToken";
        RequestToken requestToken = oauth.getOAuthRequestToken(callbackURL);

        session.setAttribute("requestToken", requestToken);

        return "redirect:" + requestToken.getAuthenticationURL();
    }

    @RequestMapping("accessToken")
    public String accessToken(Model model) throws TwitterException{
        Configuration conf = ConfigurationContext.getInstance();
        RequestToken requestToken = (RequestToken)session.getAttribute("requestToken");
        AccessToken accessToken = new AccessToken(requestToken.getToken(),requestToken.getTokenSecret());
        OAuthAuthorization oauth = new OAuthAuthorization(conf);

        oauth.setOAuthAccessToken(accessToken);
        String verifier = request.getParameter("oauth_verifier");
        accessToken = oauth.getOAuthAccessToken(verifier);

        session.setAttribute("accessToken", accessToken);

        return "redirect:mypage";
    }
}

\ 2. TwitterCntroller.javaの最終行に以下も追加定義

TwitterCntroller.java
    private Twitter createTwitter(){
        AccessToken accessToken = (AccessToken)session.getAttribute("accessToken");
        TwitterFactory factory = new TwitterFactory();
        return factory.getInstance(accessToken);
    }
}

\3. これで実行してブラウザで http://localhost:8080/requestToken にアクセスすると認証画面を追加して上記のスクリーンショットみたいな画面に遷移する(はず)

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした