Example usage for twitter4j Twitter verifyCredentials

List of usage examples for twitter4j Twitter verifyCredentials

Introduction

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

Prototype

User verifyCredentials() throws TwitterException;

Source Link

Document

Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; returns a 401 status code and an error message if not.

Usage

From source file:org.springframework.social.showcase.twitter.connect.TwitterApiAdapter.java

License:Apache License

@Override
public boolean test(Twitter api) {
    try {//ww w.java  2  s.co m
        api.verifyCredentials();
        return true;
    } catch (TwitterException e) {
        return false;
    }
}

From source file:org.springframework.social.showcase.twitter.connect.TwitterApiAdapter.java

License:Apache License

@Override
public void setConnectionValues(Twitter api, ConnectionValues values) {
    try {//ww  w  . j  a  v a2s  .com
        User user = api.verifyCredentials();
        values.setProviderUserId(Long.toString(user.getId()));
        values.setDisplayName("@" + user.getScreenName());
        values.setProfileUrl("http://twitter.com/" + user.getScreenName());
        values.setImageUrl(user.getProfileImageURL());
    } catch (TwitterException e) {
    }
}

From source file:org.springframework.social.showcase.twitter.connect.TwitterApiAdapter.java

License:Apache License

@Override
public UserProfile fetchUserProfile(Twitter api) {
    try {// w  w w.j ava  2 s .co m
        User user = api.verifyCredentials();
        return new UserProfileBuilder().setName(user.getName()).setUsername(user.getScreenName()).build();
    } catch (TwitterException e) {
        return null;
    }
}

From source file:org.twitter.oauth.java

public static void main(String args[]) throws Exception {
    // The factory instance is re-useable and thread safe.
    Twitter twitter = TwitterFactory.getSingleton();
    twitter.setOAuthConsumer("SjLUa1Pwrs81nIAGiR4f1l4I7", "ISAXBmzqzYLKWQXAaOe09j34APvVOyxahHghLBSvvR0Psnhozl");
    RequestToken requestToken = twitter.getOAuthRequestToken();
    AccessToken accessToken = null;/*  w  w w  . java 2  s .  c  o m*/
    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 aviailable) 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.
    storeAccessToken((int) twitter.verifyCredentials().getId(), accessToken);
    Status status = twitter.updateStatus(args[0]);
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
    System.exit(0);
}

From source file:org.wso2.carbon.connector.twitter.TwitterClientLoader.java

License:Open Source License

public Twitter loadApiClient() throws TwitterException {
    Twitter twitter;
    if (messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_KEY) != null
            && messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_SECRET) != null
            && messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN) != null
            && messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN_SECRET) != null) {
        ConfigurationBuilder build = new ConfigurationBuilder();
        build.setJSONStoreEnabled(true);
        build.setOAuthAccessToken(/* w w w.ja v  a2s.co m*/
                messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN).toString());
        build.setOAuthAccessTokenSecret(messageContext
                .getProperty(TwitterConnectConstants.TWITTER_USER_ACCESS_TOKEN_SECRET).toString());
        build.setOAuthConsumerKey(
                messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_KEY).toString());
        build.setOAuthConsumerSecret(
                messageContext.getProperty(TwitterConnectConstants.TWITTER_USER_CONSUMER_SECRET).toString());
        twitter = new TwitterFactory(build.build()).getInstance();
        twitter.verifyCredentials();
    } else {
        twitter = new TwitterFactory().getInstance();
    }
    return twitter;
}

From source file:org.wso2.carbon.identity.authenticator.twitter.TwitterAuthenticator.java

License:Open Source License

/**
 * Process the response of the Twitter/*from   w w  w  .  jav a  2s. com*/
 */
@Override
protected void processAuthenticationResponse(HttpServletRequest request, HttpServletResponse response,
        AuthenticationContext context) throws AuthenticationFailedException {
    Twitter twitter = (Twitter) request.getSession()
            .getAttribute(TwitterAuthenticatorConstants.AUTHENTICATOR_NAME.toLowerCase());
    RequestToken requestToken = (RequestToken) request.getSession()
            .getAttribute(TwitterAuthenticatorConstants.TWITTER_REQUEST_TOKEN);
    String verifier = request.getParameter(TwitterAuthenticatorConstants.TWITTER_OAUTH_VERIFIER);
    try {
        AccessToken token = twitter.getOAuthAccessToken(requestToken, verifier);
        request.getSession().removeAttribute(TwitterAuthenticatorConstants.TWITTER_REQUEST_TOKEN);
        User user = twitter.verifyCredentials();
        if (token != null) {
            buildClaims(user, context);
        }
    } catch (TwitterException e) {
        log.error("Exception while obtaining OAuth token form Twitter", e);
        throw new AuthenticationFailedException("Exception while obtaining OAuth token form Twitter", e);
    }
}

From source file:Origin.Mypage.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {
    response.setContentType("text/html;charset=UTF-8");
    /* TODO output your page here. You may use following sample code. */
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);/*  w ww  .ja  v a2 s.  c o m*/
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    User user = twitter.verifyCredentials();
    request.setCharacterEncoding("UTF-8");
    HttpSession hs = request.getSession();
    UserData ud = (UserData) hs.getAttribute("ud");
    String search = ud.getLine() + "?";
    //String search= request.getParameter("searchtweet");
    Query query = new Query();
    query.setCount(100);
    query.setQuery(search);
    QueryResult queryresult = null;
    try {
        queryresult = twitter.search(query);
    } catch (TwitterException e1) {
        e1.printStackTrace();
    }
    ArrayList<String> userID = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        userID.add(tweet.getUser().getScreenName());
    }
    ArrayList<String> profileimg = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        profileimg.add(tweet.getUser().getBiggerProfileImageURL());
    }
    ArrayList<String> username = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName());
    }
    ArrayList<String> usernameURL = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getURL());
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    ArrayList<String> resulttweet = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt()));
    }
    request.setAttribute("search", search);
    request.setAttribute("userID", userID);
    request.setAttribute("profileimg", profileimg);
    request.setAttribute("username", username);
    request.setAttribute("resulttweet", resulttweet);
    request.setAttribute("usernameURL", usernameURL);
    request.getRequestDispatcher("/mypage.jsp").forward(request, response);
}

From source file:Origin.Searchtweet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {
    response.setContentType("text/html;charset=UTF-8");
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);/*from www  .ja v  a 2s  .c  o  m*/
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    User user = twitter.verifyCredentials();
    request.setCharacterEncoding("UTF-8");
    String search = "?" + request.getParameter("searchtweet");
    //String search= request.getParameter("searchtweet");
    Query query = new Query();
    query.setCount(100);
    query.setQuery(search);
    QueryResult queryresult = null;
    try {
        queryresult = twitter.search(query);
    } catch (TwitterException e1) {
        e1.printStackTrace();
    }
    ArrayList<String> userID = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        userID.add(tweet.getUser().getScreenName());
    }
    ArrayList<String> profileimg = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        profileimg.add(tweet.getUser().getBiggerProfileImageURL());
    }
    ArrayList<String> username = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getName() + "<br>" + "@" + tweet.getUser().getScreenName());
    }
    ArrayList<String> usernameURL = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        username.add(tweet.getUser().getURL());
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");

    ArrayList<String> resulttweet = new ArrayList<>();
    for (Status tweet : queryresult.getTweets()) {
        resulttweet.add(tweet.getText() + "<br>" + sdf.format(tweet.getCreatedAt()));
    }
    request.setAttribute("search", search);
    request.setAttribute("userID", userID);
    request.setAttribute("profileimg", profileimg);
    request.setAttribute("username", username);
    request.setAttribute("resulttweet", resulttweet);
    request.setAttribute("usernameURL", usernameURL);
    request.getRequestDispatcher("/searchtweet.jsp").forward(request, response);
}

From source file:Origin.Timeline.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);//from w  w  w . j  av  a 2s . c  om
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    try {
        Twitter twitter = new TwitterFactory(cb.build()).getInstance();
        User user = twitter.verifyCredentials();
        Paging paging = new Paging(1, 200);
        ResponseList<Status> userstatus = twitter.getHomeTimeline(paging);
        request.setAttribute("userstatus", userstatus);
        request.getRequestDispatcher("/timeline.jsp").forward(request, response);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }

}

From source file:Origin.Tweet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, TwitterException {
    response.setContentType("text/html;charset=UTF-8");
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);//  w  ww .  j av  a2  s  . c  o m
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    try {
        Twitter twitter = new TwitterFactory(cb.build()).getInstance();
        User user = twitter.verifyCredentials();
        HttpSession session = request.getSession();
        request.setCharacterEncoding("UTF-8");
        //String tweet = encode(request.getParameter("tweet"));
        String tweet = request.getParameter("tweet");
        Status status = twitter.updateStatus(tweet);
        session.setAttribute("tweet", tweet);
        request.getRequestDispatcher("/tweet.jsp").forward(request, response);
    } catch (TwitterException e1) {
        e1.printStackTrace();
    }
}