Exemple #1
0
 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;
   }
 }
Exemple #2
0
  public Status updateStatus(String status) {
    try {
      if (authorize() == false) {
        if (authErrStatus != null) {
          return authErrStatus;
        }
      }

      StatusFeedParser parser = new StatusFeedParser();
      String url = STATUS_UPDATE_URL;
      QueryParameter[] params =
          new QueryParameter[] {
            new QueryParameter("status", status), new QueryParameter("source", "twim")
          };
      // "?status=" + URLUTF8Encoder.encode(status) +
      // "&source=twim";
      xauth.xAuthWebRequest(true, STATUS_UPDATE_URL, params, parser);
      // HttpUtil.doPost( url, parser );
      Vector statuses = parser.getStatuses();
      if (statuses != null && statuses.isEmpty() == false && status.startsWith("d ") == false) {
        return (Status) statuses.elementAt(0);
      }
    } catch (Exception ex) {
      return new Status(
          "Twim",
          "Error while updating status: " + ex.getMessage(),
          Calendar.getInstance().getTime(),
          "0");
    }
    return null;
  }
Exemple #3
0
 public void bypassAuthorization(String token, String tokenSecret) {
   if (token != null && token.length() > 0) {
     xauth = new XAuth(username, password);
     xauth.setTokenAndSecret(token, tokenSecret);
     isAuthenticated = true;
   } else {
     isAuthenticated = false;
   }
 }
Exemple #4
0
 public String unfollowUser(Status status) throws Exception {
   try {
     NullParser parser = new NullParser();
     String url = FRIENDSHIPS_DESTROY_URL + status.getScreenName() + ".xml";
     xauth.xAuthWebRequest(true, url, null, parser);
     return parser.getResponse();
   } catch (Exception ex) {
     throw ex;
   }
 }
Exemple #5
0
 /**
  * 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;
 }
Exemple #6
0
  public Vector requestLists() throws Exception {
    authorize();

    Vector entries = new Vector();
    try {
      ListsParser parser = new ListsParser();
      String url = StringUtil.replace(LISTS_URL, "@USERNAME@", username);
      xauth.xAuthWebRequest(false, url, null, parser);
      // HttpUtil.doGet(url, parser);
      entries = parser.getUserLists();
    } catch (IOException ex) {
      throw new IOException("Error in TwitterApi.requestLists: " + ex.getMessage());
    } catch (Exception ex) {
      throw new Exception("Error in TwitterApi.requestLists: " + ex.getMessage());
    }
    return entries;
  }
Exemple #7
0
 public Status markAsUnfavorite(Status status) {
   try {
     StatusFeedParser parser = new StatusFeedParser();
     String url = FAVORITE_DESTROY_URL + status.getId() + ".xml";
     xauth.xAuthWebRequest(true, url, null, parser);
     // HttpUtil.doPost( url, parser );
     Vector statuses = parser.getStatuses();
     if (statuses != null && statuses.isEmpty() == false) {
       return (Status) statuses.elementAt(0);
     }
   } catch (Exception ex) {
     return new Status(
         "Twim",
         "Error while marking status as unfavorite: " + ex.getMessage(),
         Calendar.getInstance().getTime(),
         "0");
   }
   return null;
 }
Exemple #8
0
 void resetToken() {
   xauth.setTokenAndSecret("", "");
   isAuthenticated = false;
 }
Exemple #9
0
  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;
  }