Exemplo n.º 1
0
  public void testDesktopClient() throws Exception {
    RequestToken rt;
    Twitter twitter = new Twitter();
    HttpClient http;
    HttpResponse response;
    String resStr;
    String authorizeURL;
    HttpParameter[] params;
    AccessToken at;
    String cookie;
    http = new HttpClient();

    // desktop client - requiring pin
    unauthenticated.setOAuthConsumer(desktopConsumerKey, desktopConsumerSecret);
    rt = unauthenticated.getOAuthRequestToken();
    // trying to get an access token without permitting the request token.
    try {
      rt.getAccessToken();
      fail();
    } catch (TwitterException te) {
      assertEquals(401, te.getStatusCode());
    }
    twitter.setOAuthConsumer(desktopConsumerKey, desktopConsumerSecret);
    rt = twitter.getOAuthRequestToken();
    // trying to get an access token without permitting the request token.
    try {
      twitter.getOAuthAccessToken(rt.getToken(), rt.getTokenSecret());
      fail();
    } catch (TwitterException te) {
      assertEquals(401, te.getStatusCode());
    }
    Map<String, String> props = new HashMap<String, String>();
    response = http.get(rt.getAuthorizationURL());
    cookie = response.getResponseHeader("Set-Cookie");
    //        http.setRequestHeader("Cookie", cookie);
    props.put("Cookie", cookie);
    resStr = response.asString();
    authorizeURL = catchPattern(resStr, "<form action=\"", "\" id=\"login_form\"");
    params = new HttpParameter[4];
    params[0] =
        new HttpParameter(
            "authenticity_token",
            catchPattern(resStr, "\"authenticity_token\" type=\"hidden\" value=\"", "\" />"));
    params[1] =
        new HttpParameter(
            "oauth_token",
            catchPattern(resStr, "name=\"oauth_token\" type=\"hidden\" value=\"", "\" />"));
    params[2] = new HttpParameter("session[username_or_email]", id1.screenName);
    params[3] = new HttpParameter("session[password]", id1.pass);
    response = http.request(new HttpRequest(RequestMethod.POST, authorizeURL, params, null, props));
    resStr = response.asString();
    String pin = catchPattern(resStr, "<div id=\"oauth_pin\">\n  ", "\n</div>");
    at = twitter.getOAuthAccessToken(rt.getToken(), rt.getTokenSecret(), pin);
    assertEquals(at.getScreenName(), id1.screenName);
    assertEquals(at.getUserId(), 6358482);
  }
Exemplo n.º 2
0
  public void testBrowserClient() throws Exception {
    RequestToken rt;
    Twitter twitter = new Twitter();
    HttpClient http;
    HttpResponse response;
    String resStr;
    String authorizeURL;
    HttpParameter[] params;
    AccessToken at;
    String cookie;
    http = new HttpClient();

    // browser client - not requiring pin
    twitter.setOAuthConsumer(browserConsumerKey, browserConsumerSecret);
    rt = twitter.getOAuthRequestToken();

    response = http.get(rt.getAuthorizationURL());

    response = http.get(rt.getAuthorizationURL());
    Map<String, String> props = new HashMap<String, String>();
    cookie = response.getResponseHeader("Set-Cookie");
    //        http.setRequestHeader("Cookie", cookie);
    props.put("Cookie", cookie);
    resStr = response.asString();
    authorizeURL = catchPattern(resStr, "<form action=\"", "\" id=\"login_form\"");
    params = new HttpParameter[4];
    params[0] =
        new HttpParameter(
            "authenticity_token",
            catchPattern(resStr, "\"authenticity_token\" type=\"hidden\" value=\"", "\" />"));
    params[1] =
        new HttpParameter(
            "oauth_token",
            catchPattern(resStr, "name=\"oauth_token\" type=\"hidden\" value=\"", "\" />"));
    params[2] = new HttpParameter("session[username_or_email]", id1.screenName);
    params[3] = new HttpParameter("session[password]", id1.pass);
    response = http.request(new HttpRequest(RequestMethod.POST, authorizeURL, params, null, props));
    at = twitter.getOAuthAccessToken(rt.getToken(), rt.getTokenSecret());
    assertEquals(at.getScreenName(), id1.screenName);
    assertEquals(at.getUserId(), 6358482);
  }