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:org.jwebsocket.plugins.twitter.TwitterPlugIn.java

License:Apache License

private boolean mCheckAuth(Token aToken) {
    String lMsg;//w ww. jav  a  2  s .  c  om
    try {
        if (mTwitterFactory == null) {
            if (mLog.isDebugEnabled()) {
                mLog.debug("Authenticating against Twitter...");
            }
            // The factory instance is re-useable and thread safe.
            /*
             ConfigurationBuilder lCB = new ConfigurationBuilder();
             lCB.setDebugEnabled(true);
             lCB.setOAuthConsumerKey(mSettings.getConsumerKey());
             lCB.setOAuthConsumerSecret(mSettings.getConsumerSecret());
             lCB.setOAuthAccessToken(mSettings.getAccessKey());
             lCB.setOAuthAccessTokenSecret(mSettings.getAccessSecret());
             mTwitterFactory = new TwitterFactory(lCB.build());
             */
            mTwitterFactory = new TwitterFactory();
            mTwitter = mTwitterFactory.getInstance();
            // mTwitter.setOAuthConsumer(mSettings.getConsumerKey(), mSettings.getConsumerSecret());

            // AccessToken lAccessToken = new AccessToken(mSettings.getAccessKey(), mSettings.getAccessSecret());
            // mTwitter.setOAuthAccessToken(lAccessToken);
            lMsg = "Successfully authenticated against Twitter.";
        } else {
            lMsg = "Already authenticated against Twitter.";
        }
        if (mLog.isInfoEnabled()) {
            mLog.info(lMsg);
        }
        return true;
    } catch (Exception lEx) {
        lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage();
        aToken.setInteger("code", -1);
        aToken.setString("msg", lMsg);
        mLog.error(lMsg);
    }
    return false;
}

From source file:org.jwebsocket.plugins.twitter.TwitterPlugIn.java

License:Apache License

private void requestAccessToken(WebSocketConnector aConnector, Token aToken) {
    TokenServer lServer = getServer();/*from w w w  .  j a  va  2 s.c om*/

    // instantiate response token
    Token lResponse = lServer.createResponse(aToken);
    String lMsg;
    String lCallbackURL = aToken.getString("callbackURL");
    try {
        if (!mCheckAuth(lResponse)) {
            mLog.error(lResponse.getString("msg"));
        } else {
            // get a new Twitter object for the user
            TwitterFactory lTwitterFactory = new TwitterFactory();
            Twitter lTwitter = lTwitterFactory.getInstance();
            lTwitter.setOAuthConsumer(mSettings.getConsumerKey(), mSettings.getConsumerSecret());

            // pass callback URL to Twitter API if given
            RequestToken lReqToken = (lCallbackURL != null ? lTwitter.getOAuthRequestToken(lCallbackURL)
                    : lTwitter.getOAuthRequestToken());

            String lAuthenticationURL = lReqToken.getAuthenticationURL();
            String lAuthorizationURL = lReqToken.getAuthorizationURL();

            lResponse.setString("authenticationURL", lAuthenticationURL);
            lResponse.setString("authorizationURL", lAuthorizationURL);
            lMsg = "authenticationURL: " + lAuthenticationURL + ", authorizationURL: " + lAuthorizationURL;
            lResponse.setString("msg", lMsg);
            if (mLog.isInfoEnabled()) {
                mLog.info(lMsg);
            }

            aConnector.setVar(OAUTH_REQUEST_TOKEN, lReqToken);
            aConnector.setVar(TWITTER_VAR, lTwitter);
        }
    } catch (Exception lEx) {
        lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage();
        mLog.error(lMsg);
        lResponse.setInteger("code", -1);
        lResponse.setString("msg", lMsg);
    }

    // send response to requester
    lServer.sendToken(aConnector, lResponse);
}

From source file:org.kawalpemilukada.login.login.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from ww w. j  a v a2 s .c  o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String form_action = request.getParameter("form_action");
    if (form_action == null) {
        form_action = "";
    }
    PrintWriter out = response.getWriter();
    if (form_action.equalsIgnoreCase("loginfb")) {
        String tahun = request.getParameter("tahun");
        if (tahun == null) {
            tahun = "";
        }
        Facebook facebook = new FacebookFactory().getInstance();
        request.getSession().setAttribute("facebook", facebook);
        request.getSession().setAttribute("tahun", tahun);
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/callbackfb");
        response.sendRedirect(facebook.getOAuthAuthorizationURL(callbackURL.toString()) + "&display=popup");
    }
    if (form_action.equalsIgnoreCase("logintwit")) {
        String tahun = request.getParameter("tahun");
        if (tahun == null) {
            tahun = "";
        }
        Twitter twitter = new TwitterFactory().getInstance();
        request.getSession().setAttribute("twitter", twitter);
        request.getSession().setAttribute("tahun", tahun);
        String rurl = request.getParameter("rurl");
        StringBuffer callbackURL = request.getRequestURL();
        int index = callbackURL.lastIndexOf("/");
        callbackURL.replace(index, callbackURL.length(), "").append("/callbackTwit?rurl=" + rurl);
        try {
            RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL.toString());
            request.getSession().setAttribute("requestToken", requestToken);
            response.sendRedirect(requestToken.getAuthenticationURL());
        } catch (TwitterException e) {
            request.getSession().removeAttribute("twitter");
            request.getSession().removeAttribute("tahun");
        }
    }

    if (form_action.equalsIgnoreCase("loginmobiletwit")) {
        String t = "twit";
        UserData user = CommonServices.getUser(request);
        LinkedHashMap record = new LinkedHashMap();
        Gson gson = new Gson();
        StringBuffer sb = new StringBuffer();
        String line = null;
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject input = (JSONObject) JSONValue.parse(sb.toString());
        if (user == null) {
            try {
                JSONObject twit = (JSONObject) input.get("user");
                String id = CommonServices.getVal(twit.get("id"));
                id = id.replaceAll(t, "");
                user = ofy().load().type(UserData.class).id(t + id).now();
                if (user == null) {
                    String accessToken = CommonServices.getVal(twit.get("token"));
                    String accessTokenSecret = CommonServices.getVal(twit.get("secret"));
                    String consumerKey = new CommonServices().getPropValues("kpu.properties", "consumerKey",
                            request);
                    String consumerSecret = new CommonServices().getPropValues("kpu.properties",
                            "consumerSecret", request);
                    ConfigurationBuilder cb = new ConfigurationBuilder();
                    cb.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)
                            .setOAuthConsumerSecret(consumerSecret).setOAuthAccessToken(accessToken)
                            .setOAuthAccessTokenSecret(accessTokenSecret);
                    Twitter twitter = new TwitterFactory(cb.build()).getInstance();
                    User u = twitter.showUser(twitter.getId());
                    user = new UserData("twit" + CommonServices.getVal(twitter.getId()));
                    user.imgurl = u.getBiggerProfileImageURLHttps().replace("http://", "https://");
                    user.nama = CommonServices.getVal(u.getName());
                    user.link = "https://twitter.com/" + CommonServices.getVal(twitter.getScreenName());
                    user.email = "";
                    user.uuid = CommonServices.getVal(twit.get("uuid"));
                    user.type = t;
                    ofy().save().entity(user).now();
                    Dashboard dashboard = CommonServices.getDashboard(CommonServices.setParentId("2015", "0"));
                    Dashboard dashboard2014 = CommonServices
                            .getDashboard(CommonServices.setParentId("2014", "0"));
                    dashboard.users = CommonServices.getuserSize() + "";
                    ofy().save().entity(dashboard).now();
                    dashboard2014.users = dashboard.users + "";
                    ofy().save().entity(dashboard2014).now();
                } else {
                    user.lastlogin = CommonServices.JakartaTime();
                    user.uuid = CommonServices.getVal(twit.get("uuid"));
                    if (user.link.equalsIgnoreCase("https://twitter.com/kawalpemilukada")
                            || user.link.equalsIgnoreCase(
                                    "https://www.facebook.com/app_scoped_user_id/10153164750839760/")) {
                        user.userlevel = 100000000;
                    }
                    ofy().save().entity(user).now();
                }
                request.getSession().setAttribute("UserData", JSONValue.parse(gson.toJson(user)));
                MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid);
                JSONObject device = (JSONObject) input.get("device");
                mobileSession.platform = CommonServices.getVal(device.get("platform"));
                mobileSession.version = CommonServices.getVal(device.get("version"));
                mobileSession.cordova = CommonServices.getVal(device.get("cordova"));
                mobileSession.model = CommonServices.getVal(device.get("model"));
                mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer"));
                mobileSession.appversion = CommonServices.getVal(device.get("appversion"));
                ofy().save().entity(mobileSession).now();
                record.put("sessionid", mobileSession.uuid);
            } catch (Exception e) {
            }
            record.put("sumber", "bukan dari session");
        } else {
            MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid);
            JSONObject device = (JSONObject) input.get("device");
            mobileSession.platform = CommonServices.getVal(device.get("platform"));
            mobileSession.version = CommonServices.getVal(device.get("version"));
            mobileSession.cordova = CommonServices.getVal(device.get("cordova"));
            mobileSession.model = CommonServices.getVal(device.get("model"));
            mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer"));
            mobileSession.appversion = CommonServices.getVal(device.get("appversion"));
            ofy().save().entity(mobileSession).now();
            record.put("sessionid", mobileSession.uuid);
            record.put("sumber", "dari session");
        }
        record.put("user", JSONValue.parse(gson.toJson(user)));
        response.setContentType("text/html;charset=UTF-8");
        out.print(JSONValue.toJSONString(record));
        out.flush();
    }
    if (form_action.equalsIgnoreCase("loginmobilefb")) {
        UserData user = CommonServices.getUser(request);
        LinkedHashMap record = new LinkedHashMap();
        Gson gson = new Gson();
        StringBuffer sb = new StringBuffer();
        String line = null;
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONObject input = (JSONObject) JSONValue.parse(sb.toString());
        if (user == null) {
            String t = "fb";
            JSONObject fb = (JSONObject) input.get("user");
            String id = CommonServices.getVal(fb.get("id"));
            id = id.replaceAll(t, "");
            try {
                user = ofy().load().type(UserData.class).id(t + id).now();
                if (user == null) {
                    user = new UserData("fb" + CommonServices.getVal(fb.get("id")));
                    user.imgurl = "https://graph.facebook.com/" + fb.get("id") + "/picture";
                    user.nama = CommonServices.getVal(fb.get("name"));
                    user.link = CommonServices.getVal(fb.get("link"));
                    user.email = CommonServices.getVal(fb.get("email"));
                    user.type = t;
                    user.uuid = CommonServices.getVal(fb.get("uuid"));
                    ofy().save().entity(user).now();
                    Dashboard dashboard = CommonServices.getDashboard(CommonServices.setParentId("2015", "0"));
                    Dashboard dashboard2014 = CommonServices
                            .getDashboard(CommonServices.setParentId("2014", "0"));
                    dashboard.users = CommonServices.getuserSize() + "";
                    ofy().save().entity(dashboard).now();
                    dashboard2014.users = dashboard.users + "";
                    ofy().save().entity(dashboard2014).now();
                } else {
                    user.lastlogin = CommonServices.JakartaTime();
                    user.uuid = CommonServices.getVal(fb.get("uuid"));
                    if (user.link.equalsIgnoreCase("https://twitter.com/kawalpemilukada")
                            || user.link.equalsIgnoreCase(
                                    "https://www.facebook.com/app_scoped_user_id/10153164750839760/")) {
                        user.userlevel = 100000000;
                    }
                    ofy().save().entity(user).now();
                }
                request.getSession().setAttribute("UserData", JSONValue.parse(gson.toJson(user)));
                MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid);
                JSONObject device = (JSONObject) input.get("device");
                mobileSession.platform = CommonServices.getVal(device.get("platform"));
                mobileSession.version = CommonServices.getVal(device.get("version"));
                mobileSession.cordova = CommonServices.getVal(device.get("cordova"));
                mobileSession.model = CommonServices.getVal(device.get("model"));
                mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer"));
                mobileSession.appversion = CommonServices.getVal(device.get("appversion"));
                ofy().save().entity(mobileSession).now();
                record.put("sessionid", mobileSession.uuid);
            } catch (Exception e) {
            }
            record.put("sumber", "bukan dari session");
        } else {
            record.put("sumber", "dari session");
            MobileSession mobileSession = new MobileSession(user.uid + "#" + user.uuid);
            JSONObject device = (JSONObject) input.get("device");
            mobileSession.platform = CommonServices.getVal(device.get("platform"));
            mobileSession.version = CommonServices.getVal(device.get("version"));
            mobileSession.cordova = CommonServices.getVal(device.get("cordova"));
            mobileSession.model = CommonServices.getVal(device.get("model"));
            mobileSession.manufacturer = CommonServices.getVal(device.get("manufacturer"));
            mobileSession.appversion = CommonServices.getVal(device.get("appversion"));
            ofy().save().entity(mobileSession).now();
            record.put("sessionid", mobileSession.uuid);
        }
        record.put("user", JSONValue.parse(gson.toJson(user)));
        response.setContentType("text/html;charset=UTF-8");
        out.print(JSONValue.toJSONString(record));
        out.flush();
    }
    if (form_action.equalsIgnoreCase("cekauth")) {
        UserData user = CommonServices.getUser(request);
        LinkedHashMap record = new LinkedHashMap();
        Gson gson = new Gson();
        if (user == null) {
            record.put("status", "belum login");
        } else {
            record.put("user", JSONValue.parse(gson.toJson(user)));
            if (user.terverifikasi.equalsIgnoreCase("Y")) {
                record.put("status", "terverifikasi");
            } else {
                record.put("status", "Data Anda belum terverifikasi.");
            }
        }
        response.setContentType("text/html;charset=UTF-8");
        out.print(JSONValue.toJSONString(record));
        out.flush();
    }
    if (form_action.equalsIgnoreCase("verifikasi")) {
        LinkedHashMap record = new LinkedHashMap();
        StringBuffer sb = new StringBuffer();
        String line = null;
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        JSONArray input = (JSONArray) JSONValue.parse(sb.toString());
        String nama = "";
        String jenis_kelamin = "";
        String no_tps = "";
        try {
            String url = new CommonServices().getPropValues("kpu.properties", "verifikasiURL", request);
            String inputx = getWeb(url + input.get(0).toString());
            JSONObject data = (JSONObject) JSONValue.parse(inputx);
            nama = data.get("nama").toString();
            jenis_kelamin = data.get("jenis_kelamin").toString();
        } catch (Exception e) {
            nama = "";
        }
        if (nama == null || nama.equalsIgnoreCase("")) {
            try {
                String url = new CommonServices().getPropValues("kpu.properties", "verifikasiURL2015", request);
                String inputx = getWeb(url + input.get(0).toString());
                LinkedHashMap data = getJsonNik(inputx);
                nama = data.get("nama").toString();
                jenis_kelamin = data.get("jenis kelamin").toString();
                no_tps = data.get("no_tps").toString();
            } catch (Exception e) {
                nama = "";
            }
        }
        JSONArray matchs = new JSONArray();
        if (nama.equalsIgnoreCase(input.get(1).toString())) {
            UserData user = CommonServices.getUser(request);
            double match = 0;
            //if (data.get("nama").toString().equalsIgnoreCase(user.nama)) {
            //match = 100;
            //} else {
            String[] sosialnamaParts = user.nama.toString().split(" ");
            String[] namaParts = nama.toString().split(" ");
            int dibagi = sosialnamaParts.length;
            if (namaParts.length > dibagi) {
                dibagi = namaParts.length;
            }
            double increase = 100 / dibagi;
            String iiString = "";
            for (int i = 0; i < namaParts.length; i++) {
                String namaPart = namaParts[i];
                for (int ii = 0; ii < sosialnamaParts.length; ii++) {
                    String fbnamaPart = sosialnamaParts[ii];
                    if (namaPart.equalsIgnoreCase(fbnamaPart) && (!iiString.contains(ii + ""))) {
                        match = match + increase;
                        matchs.add(namaPart + " sesuai dengan " + fbnamaPart);
                        iiString = iiString + "[" + ii + "]";
                        break;
                    }
                }
            }
            // }
            if (match > 100) {
                match = 100;
            }
            if (match >= 60) {
                user.terverifikasi = "Y";
                user.notps = no_tps;
                ObjectifyService.ofy().save().entity(user).now();
                Gson gson = new Gson();
                record.put("user", JSONValue.parse(gson.toJson(user)));
            }
            record.put("status", match + "");
            record.put("matchs", matchs);
        } else {
            record.put("status", "Data NIK: " + input.get(0).toString() + " dan Nama: "
                    + input.get(1).toString() + " tidak ditemukan.");
        }
        response.setContentType("text/html;charset=UTF-8");
        out.print(JSONValue.toJSONString(record));
        out.flush();
    }
}

From source file:org.keycloak.social.twitter.TwitterIdentityProvider.java

License:Open Source License

@Override
public Response performLogin(AuthenticationRequest request) {
    try {/*ww  w. j  ava  2s. co m*/
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(getConfig().getClientId(), getConfig().getClientSecret());

        URI uri = new URI(request.getRedirectUri() + "?state=" + request.getState());

        RequestToken requestToken = twitter.getOAuthRequestToken(uri.toString());
        ClientSessionModel clientSession = request.getClientSession();

        clientSession.setNote("twitter_token", requestToken.getToken());
        clientSession.setNote("twitter_tokenSecret", requestToken.getTokenSecret());

        URI authenticationUrl = URI.create(requestToken.getAuthenticationURL());

        return Response.temporaryRedirect(authenticationUrl).build();
    } catch (Exception e) {
        throw new IdentityBrokerException("Could send authentication request to twitter.", e);
    }
}

From source file:org.keycloak.social.twitter.TwitterProvider.java

License:Open Source License

@Override
public AuthRequest getAuthUrl(SocialProviderConfig config, String state) throws SocialProviderException {
    try {/*from  w  w w  .  java2 s . c  o  m*/
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(config.getKey(), config.getSecret());

        URI uri = new URI(config.getCallbackUrl() + "?state=" + state);

        RequestToken requestToken = twitter.getOAuthRequestToken(uri.toString());
        return AuthRequest.create(requestToken.getAuthenticationURL())
                .setAttribute("token", requestToken.getToken())
                .setAttribute("tokenSecret", requestToken.getTokenSecret()).build();
    } catch (Exception e) {
        throw new SocialProviderException(e);
    }
}

From source file:org.keycloak.social.twitter.TwitterProvider.java

License:Open Source License

@Override
public SocialUser processCallback(SocialProviderConfig config, AuthCallback callback)
        throws SocialProviderException {
    if (callback.getQueryParam("denied") != null) {
        throw new SocialAccessDeniedException();
    }/*from w ww .  ja v  a2s .com*/

    try {
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(config.getKey(), config.getSecret());

        String token = callback.getQueryParam("oauth_token");
        String verifier = callback.getQueryParam("oauth_verifier");

        RequestToken requestToken = new RequestToken((String) callback.getAttribute("token"),
                (String) callback.getAttribute("tokenSecret"));

        twitter.getOAuthAccessToken(requestToken, verifier);
        twitter4j.User twitterUser = twitter.verifyCredentials();

        SocialUser user = new SocialUser(Long.toString(twitterUser.getId()), twitterUser.getScreenName());
        user.setName(twitterUser.getName());

        return user;
    } catch (Exception e) {
        throw new SocialProviderException(e);
    }
}

From source file:org.kuropen.elecwarnv3.util.TwitterUtilv3.java

License:Open Source License

public TwitterUtilv3(String cKey, String cSecret, String uKey, String uSecret) {
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(cKey, cSecret);
    AccessToken aToken = new AccessToken(uKey, uSecret);
    twitter.setOAuthAccessToken(aToken);
}

From source file:org.minesales.infres6.narvisAPITwiterConsole.communications.twitter.AccessTwitter.java

public static Twitter loadAccessTwitter() {
    String token = "2754794936-VRDsXE6ZxnBQeUTbprYHGEOVQcYJc1mvyNaBfyC";
    String tokenSecret = "Sb7nwzWtlmOw6CRcqRoSlXkvonZGXiLTOp9sBAlv4fDOy";
    AccessToken accessToken = new AccessToken(token, tokenSecret);
    TwitterFactory factory = new TwitterFactory();
    Twitter twitter = factory.getInstance();
    twitter.setOAuthConsumer("CkQjh66TbG4FYfry1OTrTb4tS", "v8vbUPtFAkIfSUN2KICf8GDEZ91fumVuY5mzMAmT84Ag2bVnvD");
    twitter.setOAuthAccessToken(accessToken);
    return twitter;
}