List of usage examples for twitter4j.conf ConfigurationBuilder ConfigurationBuilder
ConfigurationBuilder
From source file:org.jraf.irondad.handler.twitter.links.TwitterLinksHandler.java
License:Open Source License
private static Twitter getTwitter(HandlerContext handlerContext) { Twitter res = (Twitter) handlerContext.get("twitter"); if (res == null) { TwitterLinksHandlerConfig twitterLinksHandlerConfig = (TwitterLinksHandlerConfig) handlerContext .getHandlerConfig();// w w w .jav a2 s . c om ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.setDebugEnabled(true) .setOAuthConsumerKey(twitterLinksHandlerConfig.getOauthConsumerKey()); configurationBuilder.setOAuthConsumerSecret(twitterLinksHandlerConfig.getOauthConsumerSecret()); configurationBuilder.setOAuthAccessToken(twitterLinksHandlerConfig.getOauthAccessToken()); configurationBuilder.setOAuthAccessTokenSecret(twitterLinksHandlerConfig.getOauthAccessTokenSecret()); TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build()); res = twitterFactory.getInstance(); handlerContext.put("twitter", res); } return res; }
From source file:org.kawalpemilukada.login.login.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww. j ava 2 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.examples.broker.twitter.TwitterShowUserServlet.java
License:Apache License
@Override protected void doGet(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { TwitterOAuthResponse twitterOAuthResponse = getTwitterOAuthResponse(request); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(this.identityProvider.getConfig().get("clientId")) .setOAuthConsumerSecret(this.identityProvider.getConfig().get("clientSecret")) .setOAuthAccessToken(twitterOAuthResponse.getToken()) .setOAuthAccessTokenSecret(twitterOAuthResponse.getTokenSecret()); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();//from w ww . j av a 2 s . co m try { User user = twitter.users().showUser(twitterOAuthResponse.getScreenName()); response.setContentType(MediaType.APPLICATION_JSON); PrintWriter writer = response.getWriter(); writer.println(new ObjectMapper().writeValueAsString(user)); writer.flush(); } catch (TwitterException e) { throw new RuntimeException("Could not load social profile.", e); } }
From source file:org.loklak.harvester.TwitterAPI.java
License:Open Source License
private static TwitterFactory getUserTwitterFactory(String accessToken, String accessTokenSecret) { if (accessToken == null || accessToken.length() == 0 || accessTokenSecret == null || accessTokenSecret.length() == 0) return null; ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(DAO.getConfig("client.twitterConsumerKey", "")) .setOAuthConsumerSecret(DAO.getConfig("client.twitterConsumerSecret", "")) .setOAuthAccessToken(accessToken).setOAuthAccessTokenSecret(accessTokenSecret); cb.setJSONStoreEnabled(true);//from ww w .j av a 2 s . com return new TwitterFactory(cb.build()); }
From source file:org.loklak.scraper.TwitterRiver.java
License:Apache License
/** * Build configuration object with credentials and proxy settings * @return// w ww. j av a 2 s .c o m */ private Configuration buildTwitterConfiguration() { logger.debug("creating twitter configuration"); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(oauthConsumerKey).setOAuthConsumerSecret(oauthConsumerSecret) .setOAuthAccessToken(oauthAccessToken).setOAuthAccessTokenSecret(oauthAccessTokenSecret); if (proxyHost != null) cb.setHttpProxyHost(proxyHost); if (proxyPort != null) cb.setHttpProxyPort(Integer.parseInt(proxyPort)); if (proxyUser != null) cb.setHttpProxyUser(proxyUser); if (proxyPassword != null) cb.setHttpProxyPassword(proxyPassword); if (raw) cb.setJSONStoreEnabled(true); logger.debug("twitter configuration created"); return cb.build(); }
From source file:org.manalith.ircbot.plugin.tweetreader.TweetReader.java
License:Open Source License
private void initTwitter4j() throws TwitterException { ConfigurationBuilder builder = new ConfigurationBuilder(); Configuration config = builder.setDebugEnabled(false).setJSONStoreEnabled(true) .setOAuthConsumerKey(consumerKey).setOAuthConsumerSecret(consumerSecret).build(); tweet = new TwitterFactory(config).getInstance(); requestToken = tweet.getOAuthRequestToken(); }
From source file:org.mariotaku.twidere.activity.SignInActivity.java
License:Open Source License
private Configuration getConfiguration() { final ConfigurationBuilder cb = new ConfigurationBuilder(); final boolean enable_gzip_compressing = mPreferences.getBoolean(PREFERENCE_KEY_GZIP_COMPRESSING, false); final boolean ignore_ssl_error = mPreferences.getBoolean(PREFERENCE_KEY_IGNORE_SSL_ERROR, false); final boolean enable_proxy = mPreferences.getBoolean(PREFERENCE_KEY_ENABLE_PROXY, false); final String consumer_key = mPreferences.getString(PREFERENCE_KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY) .trim();//from w ww . j av a 2 s . c o m final String consumer_secret = mPreferences .getString(PREFERENCE_KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET).trim(); cb.setHostAddressResolver(mApplication.getHostAddressResolver()); if (mPassword == null || !mPassword.contains("*")) { cb.setHttpClientImplementation(HttpClientImpl.class); } setUserAgent(this, cb); if (!isEmpty(mRESTBaseURL)) { cb.setRestBaseURL(mRESTBaseURL); } if (!isEmpty(mOAuthBaseURL)) { cb.setOAuthBaseURL(mOAuthBaseURL); } if (!isEmpty(mSigningRESTBaseURL)) { cb.setSigningRestBaseURL(mSigningRESTBaseURL); } if (!isEmpty(mSigningOAuthBaseURL)) { cb.setSigningOAuthBaseURL(mSigningOAuthBaseURL); } if (isEmpty(consumer_key) || isEmpty(consumer_secret)) { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); } else { cb.setOAuthConsumerKey(consumer_key); cb.setOAuthConsumerSecret(consumer_secret); } cb.setGZIPEnabled(enable_gzip_compressing); cb.setIgnoreSSLError(ignore_ssl_error); if (enable_proxy) { final String proxy_host = mPreferences.getString(PREFERENCE_KEY_PROXY_HOST, null); final int proxy_port = ParseUtils.parseInt(mPreferences.getString(PREFERENCE_KEY_PROXY_PORT, "-1")); if (!isEmpty(proxy_host) && proxy_port > 0) { cb.setHttpProxyHost(proxy_host); cb.setHttpProxyPort(proxy_port); } } return cb.build(); }
From source file:org.mariotaku.twidere.activity.support.SignInActivity.java
License:Open Source License
private Configuration getConfiguration() { final ConfigurationBuilder cb = new ConfigurationBuilder(); final boolean enable_gzip_compressing = mPreferences.getBoolean(KEY_GZIP_COMPRESSING, false); final boolean ignore_ssl_error = mPreferences.getBoolean(KEY_IGNORE_SSL_ERROR, false); final boolean enable_proxy = mPreferences.getBoolean(KEY_ENABLE_PROXY, false); cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(mApplication)); cb.setHttpClientFactory(new OkHttpClientFactory(mApplication)); Utils.setClientUserAgent(this, mConsumerKey, mConsumerSecret, cb); final String apiUrlFormat = TextUtils.isEmpty(mAPIUrlFormat) ? DEFAULT_TWITTER_API_URL_FORMAT : mAPIUrlFormat;/*w ww . j a va2 s . c om*/ final String versionSuffix = mNoVersionSuffix ? null : "/1.1/"; cb.setRestBaseURL(Utils.getApiUrl(apiUrlFormat, "api", versionSuffix)); cb.setOAuthBaseURL(Utils.getApiUrl(apiUrlFormat, "api", "/oauth/")); cb.setUploadBaseURL(Utils.getApiUrl(apiUrlFormat, "upload", versionSuffix)); cb.setOAuthAuthorizationURL(Utils.getApiUrl(apiUrlFormat, null, "/oauth/authorize")); if (!mSameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } if (isEmpty(mConsumerKey) || isEmpty(mConsumerSecret)) { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY_3); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET_3); } else { cb.setOAuthConsumerKey(mConsumerKey); cb.setOAuthConsumerSecret(mConsumerSecret); } cb.setGZIPEnabled(enable_gzip_compressing); cb.setIgnoreSSLError(ignore_ssl_error); if (enable_proxy) { final String proxy_host = mPreferences.getString(KEY_PROXY_HOST, null); final int proxy_port = ParseUtils.parseInt(mPreferences.getString(KEY_PROXY_PORT, "-1")); if (!isEmpty(proxy_host) && proxy_port > 0) { cb.setHttpProxyHost(proxy_host); cb.setHttpProxyPort(proxy_port); } } return cb.build(); }
From source file:org.mariotaku.twidere.util.Utils.java
License:Open Source License
@Nullable public static Twitter getTwitterInstance(final Context context, final long accountId, final boolean includeEntities, final boolean includeRetweets) { if (context == null) return null; final TwidereApplication app = TwidereApplication.getInstance(context); final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); final int connection_timeout = prefs.getInt(KEY_CONNECTION_TIMEOUT, 10) * 1000; final boolean enableGzip = prefs.getBoolean(KEY_GZIP_COMPRESSING, true); final boolean ignoreSslError = prefs.getBoolean(KEY_IGNORE_SSL_ERROR, false); final boolean enableProxy = prefs.getBoolean(KEY_ENABLE_PROXY, false); // Here I use old consumer key/secret because it's default key for older // versions/*from w w w . j av a 2s . c om*/ final ParcelableCredentials credentials = ParcelableCredentials.getCredentials(context, accountId); if (credentials == null) return null; final ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(app)); cb.setHttpClientFactory(new OkHttpClientFactory(context)); cb.setHttpConnectionTimeout(connection_timeout); cb.setGZIPEnabled(enableGzip); cb.setIgnoreSSLError(ignoreSslError); cb.setIncludeCards(true); cb.setCardsPlatform("Android-12"); // cb.setModelVersion(7); if (enableProxy) { final String proxy_host = prefs.getString(KEY_PROXY_HOST, null); final int proxy_port = ParseUtils.parseInt(prefs.getString(KEY_PROXY_PORT, "-1")); if (!isEmpty(proxy_host) && proxy_port > 0) { cb.setHttpProxyHost(proxy_host); cb.setHttpProxyPort(proxy_port); } } final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY); final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET); final String apiUrlFormat = credentials.api_url_format; final String consumerKey = trim(credentials.consumer_key); final String consumerSecret = trim(credentials.consumer_secret); final boolean sameOAuthSigningUrl = credentials.same_oauth_signing_url; final boolean noVersionSuffix = credentials.no_version_suffix; if (!isEmpty(apiUrlFormat)) { final String versionSuffix = noVersionSuffix ? null : "/1.1/"; cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", versionSuffix)); cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/")); cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", versionSuffix)); cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null)); if (!sameOAuthSigningUrl) { cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL); cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL); cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL); } } setClientUserAgent(context, consumerKey, consumerSecret, cb); cb.setIncludeEntitiesEnabled(includeEntities); cb.setIncludeRTsEnabled(includeRetweets); cb.setIncludeReplyCountEnabled(true); cb.setIncludeDescendentReplyCountEnabled(true); switch (credentials.auth_type) { case Accounts.AUTH_TYPE_OAUTH: case Accounts.AUTH_TYPE_XAUTH: { if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) { cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) { cb.setOAuthConsumerKey(prefConsumerKey); cb.setOAuthConsumerSecret(prefConsumerSecret); } else { cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY); cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET); } final String token = credentials.oauth_token; final String tokenSecret = credentials.oauth_token_secret; if (isEmpty(token) || isEmpty(tokenSecret)) return null; return new TwitterFactory(cb.build()).getInstance(new AccessToken(token, tokenSecret)); } case Accounts.AUTH_TYPE_BASIC: { final String screenName = credentials.screen_name; final String username = credentials.basic_auth_username; final String loginName = username != null ? username : screenName; final String password = credentials.basic_auth_password; if (isEmpty(loginName) || isEmpty(password)) return null; return new TwitterFactory(cb.build()).getInstance(new BasicAuthorization(loginName, password)); } case Accounts.AUTH_TYPE_TWIP_O_MODE: { return new TwitterFactory(cb.build()).getInstance(new TwipOModeAuthorization()); } default: { return null; } } }
From source file:org.mixare.utils.TwitterClient.java
License:Open Source License
/** * Query the twitter search API using oAuth 2.0 * @return//from w w w . ja va2 s. c o m */ public static String queryData() { ConfigurationBuilder cb = new ConfigurationBuilder(); //to be configured in a properties... cb.setDebugEnabled(true).setOAuthConsumerKey("mt10dv6tTKacqlm14lw5w") .setOAuthConsumerSecret("4kRV1E1XIU3kj4JQj2R5LE1yct0RRaRl9sB5PpPrB0") .setOAuthAccessToken("390019380-IQ5VdvUKvxY9JOsTToEU8ElCabebc76H9X2g3QX4") .setOAuthAccessTokenSecret("ghJn4LTfDr7uHUCsbt6ycmpeVTwwpa3hZnXyEjyZvs"); cb.setJSONStoreEnabled(true); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance(); Query query = new Query(); query = query.geoCode(new GeoLocation(lat, lon), rad, Query.KILOMETERS); String jsonArrayAsString = "{\"results\":[";//start try { QueryResult result = twitter.search(query); int size = 0; for (Status status : result.getTweets()) { { if (status.getGeoLocation() != null) { String jsonSingleObject = DataObjectFactory.getRawJSON(status); if (size == 0) jsonArrayAsString += jsonSingleObject; else jsonArrayAsString += "," + jsonSingleObject; size++; } } } jsonArrayAsString += "]}";//close array return jsonArrayAsString; } catch (Exception e) { Log.e(Config.TAG, "Error querying twitter data :" + e); e.printStackTrace(); } return null; }