Example usage for twitter4j TwitterFactory TwitterFactory

List of usage examples for twitter4j TwitterFactory TwitterFactory

Introduction

In this page you can find the example usage for twitter4j TwitterFactory TwitterFactory.

Prototype

public TwitterFactory() 

Source Link

Document

Creates a TwitterFactory with the root configuration.

Usage

From source file:com.amazonbird.announce.AnnouncerMgrImpl.java

License:Open Source License

public boolean announcerIsvalid(Announcer announcer) {
    try {//from   ww  w.  j av a 2 s .c o  m
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret());
        twitter.setOAuthAccessToken(
                new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret()));

    } catch (Exception ex) {
        return false;
    }
    return true;

}

From source file:com.amazonbird.db.data.Announcer.java

License:Open Source License

public Twitter getTwitterProxy() throws TwitterException {
    if (twitter == null) {

        TwitterFactory tf = new TwitterFactory();
        twitter = tf.getInstance();/*  w  w w .  j a  v  a 2s .  c om*/

        twitter.setOAuthConsumer(getConsumerKey(), getConsumerSecret());
        twitter.setOAuthAccessToken(new AccessToken(getAccessToken(), getAccessTokenSecret()));
    }
    return twitter;
}

From source file:com.androtwitt.ExampleAppWidgetProvider.java

License:Apache License

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    Log.d(TAG, "updateAppWidget appWidgetId=" + appWidgetId);
    ArrayList lines = new ArrayList();

    Twitter unauthenticatedTwitter = new TwitterFactory().getInstance();
    System.out.println("Showing public timeline.");
    try {/*from w w  w.  j  av  a  2  s.  c o m*/
        List<Status> statuses = unauthenticatedTwitter.getPublicTimeline();

        // Other methods require authentication
        String username = ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId, "username");
        String password = ExampleAppWidgetConfigure.loadTitlePref(context, appWidgetId, "password");

        Twitter twitter = new TwitterFactory().getInstance(username, password);
        statuses = twitter.getFriendsTimeline();
        for (Status status : statuses) {
            lines.add(status.getUser().getScreenName() + ":" + status.getText());
        }
    } catch (TwitterException te) {
        Log.d("AT_Error", "Failed to get timeline: " + te.getMessage());
    }

    // Construct the RemoteViews object.  It takes the package name (in our case, it's our
    // package, but it needs this because on the other side it's the widget host inflating
    // the layout from our package).
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider);
    ArrayList<Integer> widgetTexts = new ArrayList();
    widgetTexts.add(R.id.appwidget_text0);
    widgetTexts.add(R.id.appwidget_text1);
    widgetTexts.add(R.id.appwidget_text2);
    widgetTexts.add(R.id.appwidget_text3);
    widgetTexts.add(R.id.appwidget_text4);
    widgetTexts.add(R.id.appwidget_text5);
    widgetTexts.add(R.id.appwidget_text6);
    widgetTexts.add(R.id.appwidget_text7);
    widgetTexts.add(R.id.appwidget_text8);
    widgetTexts.add(R.id.appwidget_text9);

    Iterator i = widgetTexts.iterator();
    Iterator j = lines.iterator();
    while (i.hasNext()) {
        Integer value = (Integer) i.next();
        if (j.hasNext()) {
            views.setTextViewText(value, (String) j.next());
        }
    }

    // Tell the widget manager
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

From source file:com.anshul.LoginServlet.java

License:Open Source License

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    try {//from w  ww .  jav  a 2 s .c om
        Twitter twitter = new TwitterFactory().getInstance();

        try {
            twitter.setOAuthConsumer("dyHSJLTZhh2eSSw2RVpqMIKyJ",
                    "jW22uPPA54DOMNicfFJWzf4jB7nKHbdi7L6VuPFlfWEPAOt5Ai");
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

        StringBuffer callbackURL = req.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/home");

        RequestToken requestToken;
        requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
        String token = requestToken.getToken();
        String tokenSecret = requestToken.getTokenSecret();

        HttpSession session = req.getSession();
        session.setAttribute("twitter", twitter);
        String authUrl = requestToken.getAuthorizationURL();
        //        session.setAttribute("authUrl", authUrl);
        req.setAttribute("authUrl", authUrl);
        RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
        rd.forward(req, resp);
    } catch (TwitterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ServletException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.appspot.bitlyminous.command.authentication.AbstractAuthenticationCommand.java

License:Apache License

/**
 * Execute twitter o auth./*from w  w w  .ja v  a 2s.c  o m*/
 * 
 * @param req the req
 * @param resp the resp
 * @param callbackUrl the callback url
 * 
 * @return true, if successful
 * 
 * @throws Exception the exception
 */
protected boolean executeTwitterOAuth(HttpServletRequest req, HttpServletResponse resp, String callbackUrl)
        throws Exception {
    HttpSession session = req.getSession(true);
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(ApplicationConstants.TWITTER_CONSUMER_KEY,
            ApplicationConstants.TWITTER_CONSUMER_SECRET);
    if (session.getAttribute(ParameterNames.REQUEST_TOKEN_PARAM) == null
            || session.getAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM) == null) {
        RequestToken requestToken = twitter.getOAuthRequestToken(callbackUrl);
        session.setAttribute(ParameterNames.REQUEST_TOKEN_PARAM, requestToken.getToken());
        session.setAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM, requestToken.getTokenSecret());
        resp.sendRedirect(requestToken.getAuthenticationURL());
        return false;
    } else {
        AccessToken accessToken = twitter.getOAuthAccessToken(
                new RequestToken((String) session.getAttribute(ParameterNames.REQUEST_TOKEN_PARAM),
                        (String) session.getAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM)),
                req.getParameter(ParameterNames.OAUTH_VERIFIER));
        session.removeAttribute(ParameterNames.REQUEST_TOKEN_PARAM);
        session.removeAttribute(ParameterNames.REQUEST_TOKEN_SECRET_PARAM);
        User user = userService.getUserByScreenName(accessToken.getScreenName());
        if (user == null) {
            user = new User();
            user.setScreenName(accessToken.getScreenName());
        }
        user.setTwitterToken(new Text(accessToken.getToken()));
        user.setTwitterSecret(new Text(accessToken.getTokenSecret()));
        session.setAttribute(ParameterNames.SESSION_USER, user);
        return true;
    }
}

From source file:com.appspot.bitlyminous.handler.twitter.NearByHandler.java

License:Apache License

/**
 * Gets the twitter client.//from w ww .j  a  va2 s.  co  m
 * 
 * @return the twitter client
 */
protected Twitter getTwitterClient() {
    return new TwitterFactory().getInstance(new OAuthAuthorization(new ConfigurationBuilder().build(),
            ApplicationConstants.TWITTER_CONSUMER_KEY, ApplicationConstants.TWITTER_CONSUMER_SECRET,
            new AccessToken(ApplicationConstants.TWITTER_ACCESS_TOKEN,
                    ApplicationConstants.TWITTER_ACCESS_TOKEN_SECRET)));
}

From source file:com.appspot.bitlyminous.handler.twitter.TweetForMeHandler.java

License:Apache License

/**
 * Gets the twitter client./*ww  w  . j  a  v  a 2s.  com*/
 * 
 * @param token the token
 * @param tokenSecret the token secret
 * 
 * @return the twitter client
 */
protected Twitter getTwitterClient(String token, String tokenSecret) {
    return new TwitterFactory().getInstance(new OAuthAuthorization(new ConfigurationBuilder().build(),
            ApplicationConstants.TWITTER_CONSUMER_KEY, ApplicationConstants.TWITTER_CONSUMER_SECRET,
            new AccessToken(token, tokenSecret)));
}

From source file:com.appspot.bitlyminous.service.impl.TwitterServiceImpl.java

License:Apache License

/**
 * Gets the twitter client./*  ww w .  j  av a2 s  .com*/
 * 
 * @return the twitter client
 */
protected Twitter getTwitterClient() {
    return new TwitterFactory().getInstance(getAuthorization());
}

From source file:com.appspot.bitlyminous.service.impl.TwitterServiceImpl.java

License:Apache License

/**
 * Gets the twitter client.//w w  w.  j a v a2  s.  c  o m
 * 
 * @param token the token
 * @param tokenSecret the token secret
 * 
 * @return the twitter client
 */
protected Twitter getTwitterClient(String token, String tokenSecret) {
    return new TwitterFactory().getInstance(getAuthorization(token, tokenSecret));
}

From source file:com.appspot.bitlyminous.standalone.TwitterExample.java

License:Apache License

/**
 * The main method./*  w  w  w. ja v a  2s .  co  m*/
 * 
 * @param args the arguments
 * 
 * @throws Exception the exception
 */
public static void main(String[] args) throws Exception {
    // The factory instance is re-useable and thread safe.
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer("671O1zUicFBCYHHYVp0XA", "cP7WvfAtxXGSaf1Sz0ksZtWGcENosFcRr46WR7a9jU");
    RequestToken requestToken = twitter.getOAuthRequestToken();
    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 available) or just hit enter.[PIN]:");
        String pin = br.readLine();
        try {
            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.");
            } else {
                te.printStackTrace();
            }
        }
    }
    // persist to the accessToken for future reference.
    System.out.println("Screen name:" + accessToken.getScreenName());
    System.out.println("Token:" + accessToken.getToken());
    System.out.println("Secret:" + accessToken.getTokenSecret());

    IDs friendsIDs = twitter.getFriendsIDs("gapinginfinity");
    System.out.println(Arrays.toString(friendsIDs.getIDs()));

    ResponseList<Status> favorites = twitter.getFavorites("gapinginfinity");
    System.out.println(favorites.size());
    System.exit(0);
}