public boolean authorize() { if (isAuthenticated) { return true; } String token = ""; try { System.out.println("Trying to authenticate"); xauth = new XAuth(username, password); token = xauth.xAuthWebRequest(false, OAUTH_ACCESS_TOKEN_URL, null, null); System.out.println("ACCESS TOKEN: " + token); if (token.indexOf("oauth_token_secret") > 0) { // Success String oauthToken = HttpUtil.parseParameter(token, "oauth_token"); String oauthTokenSecret = HttpUtil.parseParameter(token, "oauth_token_secret"); xauth.setTokenAndSecret(oauthToken, oauthTokenSecret); isAuthenticated = true; authErrStatus = null; return true; } else { // Failure authErrStatus = new Status("Twitter", "Couldn't find OAuth token from response: " + token, null, ""); isAuthenticated = false; return false; } } catch (Exception ex) { authErrStatus = new Status("Twitter", "Couldn't authenticate. Exception: " + ex.getMessage(), null, ""); isAuthenticated = false; return false; } }
public Vector search(String query, int page) throws Exception { try { SearchResultsParser parser = new SearchResultsParser(); String url = SEARCH_URL + StringUtil.urlEncode(query) + "&page=" + page; Log.debug("URL: " + url); HttpUtil.doPost(url, parser); Vector statuses = parser.getStatuses(); return statuses; } catch (Exception ex) { throw new Exception("Error while searching tweets: " + ex.getMessage()); } }
/** * Request friends from Twitter API. * * @return Vector containing friends. */ public Vector requestFriendsTimeline() throws IOException, Exception { Vector entries = new Vector(); authorize(); try { HttpUtil.setBasicAuthentication("", ""); StatusFeedParser parser = new StatusFeedParser(); xauth.xAuthWebRequest(false, FRIENDS_URL, null, parser); // HttpUtil.doGet(FRIENDS_URL, parser); entries = parser.getStatuses(); } catch (IOException ex) { throw new IOException("Error in TwitterApi.requestFriends: " + ex.getMessage()); } catch (Exception ex) { throw new Exception("Error in TwitterApi.requestFriends: " + ex.getMessage()); } return entries; }
private Vector requestTimeline(String timelineUrl) { Vector entries = new Vector(); if (authorize() == false) { if (authErrStatus != null) { entries.addElement(authErrStatus); return entries; } } try { boolean retry = false; do { // HttpUtil.setBasicAuthentication(username, password); HttpUtil.setBasicAuthentication("", ""); StatusFeedParser parser = new StatusFeedParser(); if (timelineUrl.equals(DIRECT_TIMELINE_URL)) { parser.setDirect(true); } xauth.xAuthWebRequest(false, timelineUrl, null, parser); // HttpUtil.doGet(timelineUrl, parser); int lastResponseCode = HttpUtil.getLastResponseCode(); entries = parser.getStatuses(); if (entries.isEmpty() && parser.isReallyEmpty() == false) { entries.addElement( new Status( "Twitter", "No statuses. API response from " + timelineUrl + " (" + lastResponseCode + "): " + HttpUtil.getHeaders() + " " + parser.getRawData(), Calendar.getInstance().getTime(), "")); retry = !retry; } else if (entries.isEmpty() && parser.isReallyEmpty() == true) { entries.addElement( new Status("Twitter", "No Tweets found.", Calendar.getInstance().getTime(), "")); } else { retry = false; } } while (retry); } catch (IOException ex) { entries.addElement( new Status( "Twitter", "Error occured. Please check " + "your connection or username and password.", Calendar.getInstance().getTime(), "")); entries.addElement( new Status( "Twitter", "StackTrace: " + ex.toString(), Calendar.getInstance().getTime(), "")); ex.printStackTrace(); } catch (Exception ex) { entries.addElement( new Status( "Twitter", "API exception: " + ex.toString(), Calendar.getInstance().getTime(), "")); } return entries; }