List of usage examples for twitter4j TwitterException getMessage
@Override
public String getMessage()
From source file:com.allenzheng.twittersyn.utility.impl.TwitterAPIImpl.java
License:Apache License
public String getAuthorisationUrl() throws TwitterException{ Properties prop = loadProperties(); /* w w w .j a v a 2 s. c om*/ Twitter twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(prop.getProperty("twitter.consumerkey"), prop.getProperty("twitter.consumersecret")); try { RequestToken requestToken; if (callbackUrl == null) { requestToken = twitter.getOAuthRequestToken(); } else { requestToken = twitter .getOAuthRequestToken(callbackUrl); } String authorisationUrl = requestToken .getAuthorizationURL(); // session.setAttribute(ATTR_TWITTER, twitter); // session.setAttribute(ATTR_REQUEST_TOKEN, requestToken); logger.debug("Redirecting user to " + authorisationUrl); // response.sendRedirect(authorisationUrl); return authorisationUrl; } catch (TwitterException e) { logger.error("Sign in with Twitter failed - " + e.getMessage()); e.printStackTrace(); throw new TwitterException(e); } }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
private void publishStatusUpdateWithMedia(String message) throws MalformedURLException, IOException { Status status = null;// w ww . j av a 2 s. c o m try { Twitter twitter = new TwitterFactory().getInstance(); try { RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null; while (null == accessToken) { logger.fine("Open the following URL and grant access to your account:"); logger.fine(requestToken.getAuthorizationURL()); try { accessToken = twitter.getOAuthAccessToken(requestToken); } catch (TwitterException te) { if (401 == te.getStatusCode()) { logger.severe("Unable to get the access token."); } else { te.printStackTrace(); } } } logger.log(Level.INFO, "Got access token."); logger.log(Level.INFO, "Access token: {0}", accessToken.getToken()); logger.log(Level.INFO, "Access token secret: {0}", accessToken.getTokenSecret()); } catch (IllegalStateException ie) { // access token is already available, or consumer key/secret is not set. if (!twitter.getAuthorization().isEnabled()) { logger.severe("OAuth consumer key/secret is not set."); return; } } //Instantiate and initialize a new twitter status update StatusUpdate statusUpdate = new StatusUpdate(message); //attach any media, if you want to statusUpdate.setMedia( //title of media "Amandine Leforestier Spring Summer 2015 white", new URL("https://issuu.com/kadiemurphy/docs/volume_xxi_issuu/52?e=0").openStream()); //tweet or update status status = twitter.updateStatus(statusUpdate); //Status status = twitter.updateStatus(message); logger.log(Level.INFO, "Successfully updated the status to [{0}].", status.getText()); } catch (TwitterException te) { te.printStackTrace(); logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage()); } }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
public Twitter getTwitter() { Twitter twitter = new TwitterFactory().getInstance(); try {/*from w w w. jav a2 s.c o m*/ RequestToken requestToken = twitter.getOAuthRequestToken(); AccessToken accessToken = null; while (null == accessToken) { logger.fine("Open the following URL and grant access to your account:"); logger.fine(requestToken.getAuthorizationURL()); try { accessToken = twitter.getOAuthAccessToken(requestToken); } catch (TwitterException te) { if (401 == te.getStatusCode()) { logger.severe("Unable to get the access token."); } else { logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage()); } } } logger.log(Level.INFO, "Got access token."); logger.log(Level.INFO, "Access token: {0}", accessToken.getToken()); logger.log(Level.INFO, "Access token secret: {0}", accessToken.getTokenSecret()); } catch (IllegalStateException ie) { // access token is already available, or consumer key/secret is not set. if (!twitter.getAuthorization().isEnabled()) { logger.log(Level.SEVERE, "OAuth consumer key/secret is not set: {0}", ie.getMessage()); return null; } } catch (TwitterException te) { logger.log(Level.SEVERE, "OAuth request token: {0}", te.getMessage()); } return twitter; }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
public void getFollowers(String username) { Twitter twitterHandle = this.getTwitter(); long cursor = -1; PagableResponseList<User> followers = null; //do {/*from w ww . j a va2s .c om*/ try { followers = twitterHandle.getFollowersList(URLEncoder.encode(username, "UTF-8"), cursor); } catch (TwitterException ex) { //ex.printStackTrace(); logger.log(Level.SEVERE, "Cannot get followers", ex.getMessage()); return; } catch (UnsupportedEncodingException ex) { Logger.getLogger(Tweet.class.getName()).log(Level.SEVERE, null, ex); } for (User follower : followers) { // TODO: Collect top 10 followers here System.out.println(follower.getName() + " has " + follower.getFollowersCount() + " follower(s)"); } //} while ((cursor = followers.getNextCursor()) != 0); }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
public void tweetMessageToUser(String username, String hashtags, String imageUrl, String targetUrl, String twitterid) {// www . j a v a 2 s.com Twitter twitterHandle = this.getTwitter(); //Instantiate and initialize a new twitter status update String message = username + " " + targetUrl + " " + hashtags + " #amandineleforestier"; StatusUpdate statusUpdate = new StatusUpdate(message); try { //attach any media, if you want to statusUpdate.setMedia(//title of media "Amandine Leforestier Athleasure Sport-Chic Autumn Winter 2015 http://shop.amandineleforestier.fr", new URL(imageUrl).openStream()); } catch (MalformedURLException ex) { logger.log(Level.SEVERE, "Bad image Url {0}", ex.getMessage()); } catch (IOException ex) { logger.log(Level.SEVERE, "Cannot open Url {0}", ex.getMessage()); } //tweet or update status Status status = null; try { status = twitterHandle.updateStatus(statusUpdate); logTheStatusUpdate(twitterid, message, imageUrl, targetUrl); } catch (TwitterException te) { logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage()); } //Status status = twitter.updateStatus(message); if (status != null) { logger.log(Level.INFO, "Successfully updated the status to [{0}].", status.getText()); } else { logger.log(Level.SEVERE, "Status update failed [{0}].", status); } }
From source file:com.amandine.twitterpostforcoucou.Tweet.java
public void getFollowersInformation(String FollowersFor) { try {//from w ww . ja va 2 s.c o m Twitter twitter = new TwitterFactory().getInstance(); long cursor = -1; IDs ids; System.out.println("Listing followers's ids."); do { // if (0 < args.length) { ids = twitter.getFollowersIDs(URLEncoder.encode(FollowersFor), cursor); // } else { // ids = twitter.getFollowersIDs(cursor); // } for (long id : ids.getIDs()) { //System.out.println(id + " " + twitter.showUser(id).getScreenName()); //twitter.showUser(id).getDescription language location name //twitter.showUser(id).getScreenName() this is rate limited at 180 every 15 minutes writeUsersTwitterIdToUserTable(Long.toString(id), "", FollowersFor); } try { Thread.sleep(1000 * 60); } catch (InterruptedException ex) { logger.log(Level.INFO, "Woke up", ex); } } while ((cursor = ids.getNextCursor()) != 0); System.exit(0); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to get followers' ids: " + te.getMessage()); System.exit(-1); } }
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 {// w ww . j a v a 2 s. co 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.aremaitch.codestock2010.library.TwitterLib.java
License:Apache License
public boolean sendRatingDM(String dmText) { boolean result = false; try {/*w w w . j a va2s . c o m*/ t.sendDirectMessage(TwitterConstants.CODESTOCK_USERID, dmText); result = true; ACLogger.info(CSConstants.LOG_TAG, "successfully sent dm rating: " + dmText); } catch (TwitterException e) { if (e.getStatusCode() == 403) { ACLogger.info(CSConstants.LOG_TAG, "could not dm rating: " + e.getMessage()); } else { e.printStackTrace(); } } return result; }
From source file:com.avishkar.NewGetFollowersIDs.java
License:Apache License
private static List<Long> getUserFollwers(long twitterUserId) throws UnknownHostException, TwitterException, InterruptedException { Twitter twitter = new TwitterFactory(AccessTokenUtil.getConfig()).getInstance(); List<Long> filteredUserListOnFollowerCount = new ArrayList<Long>(); final Gson gson = new Gson(); try {//from www.jav a2 s .co m checkRateLimit("/followers/ids", twitter); System.out.println("Listing followers's Follower for ID:" + twitterUserId + System.lineSeparator()); IDs followerIDs = twitter.getFollowersIDs(twitterUserId, -1); JsonObject followerJSON = new JsonObject(); followerJSON.addProperty("id", twitterUserId); followerJSON.addProperty("followers", gson.toJson(followerIDs.getIDs())); DBAccess.insert(gson.toJson(followerJSON)); System.out.println("Current Followers Fetched Size:" + followerIDs.getIDs().length); // Filtering for influential user if (followerIDs.getIDs().length > 1000) { System.out.println("User assumed as Influential"); return null; } int from = 0; int to = 99; int limit = checkRateLimit("/users/lookup", twitter); while (from < followerIDs.getIDs().length) { if (to > followerIDs.getIDs().length) to = followerIDs.getIDs().length - 1; if (limit == 0) checkRateLimit("/users/lookup", twitter); ResponseList<User> followers = twitter .lookupUsers(Arrays.copyOfRange(followerIDs.getIDs(), from, to)); System.out.println("Recieved User count:" + followers.size()); for (User user : followers) { DBAccess.insertUser(gson.toJson(user)); if (user.getFollowersCount() < 1000) { // if(user.getStatusesCount()>0 && // user.getStatus()!=null && // sevenDaysAgo.after(user.getStatus().getCreatedAt())) filteredUserListOnFollowerCount.add(user.getId()); getStatuses(user.getScreenName(), twitter); } else System.out.println("User " + user.getScreenName() + " is pruned for Influential or over subscription." + " Follower count:" + user.getFollowersCount() + " Friends Count:" + user.getFriendsCount()); } from += 100; to += 100; limit--; } } catch (TwitterException te) { if (te.getStatusCode() == HttpResponseCode.UNAUTHORIZED || te.getStatusCode() == HttpResponseCode.NOT_FOUND) { System.out.println("Encountered locked profile. Skipping " + twitterUserId); return null; // log something here } te.printStackTrace(); System.out.println("Failed to get followers' Follower: " + te.getMessage()); // System.exit(-1); } return filteredUserListOnFollowerCount; }
From source file:com.borqs.qiupu.ui.bpc.BeamDataByNFCActivity.java
License:Apache License
private void getUserInfo(final long userid) { if (!AccountServiceUtils.isAccountReady()) { return;//from w w w. j a v a 2 s .c om } synchronized (mInfoLock) { if (inInfoProcess == true) { Log.d(TAG, "in loading info data"); return; } } if (isUiLoading() == false) { begin(); } synchronized (mInfoLock) { inInfoProcess = true; } asyncQiupu.getUserInfo(userid, getSavedTicket(), new TwitterAdapter() { public void getUserInfo(QiupuUser user) { Log.d(TAG, "finish getUserInfo=" + user); mUser = user; synchronized (mInfoLock) { inInfoProcess = false; } // update database if ((user.circleId != null && user.circleId.length() > 0) || userid == AccountServiceUtils.getBorqsAccountID()) { orm.insertUserinfo(user); // create share source data, need it is my friends. if (mUser.circleId != null && mUser.circleId.length() > 0) { orm.updateShareSourceDB(userid, user.sharedResource); } } Message msg = mHandler.obtainMessage(GET_USER_FROM_SERVER_END); msg.getData().putBoolean(RESULT, true); msg.sendToTarget(); } public void onException(TwitterException ex, TwitterMethod method) { synchronized (mInfoLock) { inInfoProcess = false; } TwitterExceptionUtils.printException(TAG, "getUserInfo, server exception:", ex, method); Log.d(TAG, "fail to load user info=" + ex.getMessage()); Message msg = mHandler.obtainMessage(GET_USER_FROM_SERVER_END); msg.getData().putString(ERROR_MSG, ex.getMessage()); msg.getData().putBoolean(RESULT, false); msg.sendToTarget(); } }); }