在Maven项目上使用Twitter4J库发布推文

时间:2017-05-11 12:59:48

标签: java maven jsp servlets twitter

我正在为大学项目开发​​网络mashup。问题是我不能使用Twitter4J库在Twitter上发布内容。

首先,我在.jsp文件上有一个按钮,编码如下:

<form class="form" method="post" action="PostTweetController">   
    <button class="btn" type="submit" name="req" value="tweet">Tweet this</button>
    </form>     

在web.xml上使用以下servlet映射

<servlet>
    <description></description>
    <display-name>PostTweetController</display-name>
    <servlet-name>PostTweetController</servlet-name>
    <servlet-class>aiss.controller.twitter.PostTweetController</servlet-class>
</servlet>
  <servlet-mapping>
    <servlet-name>PostTweetController</servlet-name>
    <url-pattern>/PostTweetController</url-pattern>
  </servlet-mapping>

这是PostTweetController.java servlet类,我尝试编写代码,这是没有完成任务的类。

package aiss.controller.twitter;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

/**
 * Servlet implementation class IndexSearchController
 */
public class PostTweetController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostTweetController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

使用我已经完成的其他工作就像向API请求信息或在doGet方法上发布内容一样简单,但在这种情况下,我需要使用OAuth进行身份验证,这就是为什么我&#39 ;我试图使用Twitter4J,但我无法使其发挥作用。

我试过这段代码,但没有任何反应,我想它不会对servlet起作用,或者我做错了什么。

String consumerKey = "";
        String consumerSecret = "";
        String accessToken = "";
        String accessSecret = "";

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
            .setOAuthConsumerKey(consumerKey)
            .setOAuthConsumerSecret(consumerSecret)
            .setOAuthAccessToken(accessToken)
            .setOAuthAccessTokenSecret(accessSecret);
        String tweet = "Hi";
        try 
        {
           TwitterFactory factory = new TwitterFactory(cb.build());
           Twitter twitter = factory.getInstance();

           System.out.println(twitter.getScreenName());
           Status status = twitter.updateStatus(tweet);
           System.out.println("Successfully updated the status to [" + status.getText() + "].");
            }catch (TwitterException te) {
               te.printStackTrace();
               System.exit(-1);
            }

我还将所需的依赖项添加到pom.xml

0 个答案:

没有答案