Пример #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);
  }
Пример #2
0
  public void testBrowserClientWithCustomCallback() 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 / overriding callback url
    twitter = new Twitter();
    twitter.setOAuthConsumer(browserConsumerKey, browserConsumerSecret);
    rt = twitter.getOAuthRequestToken("http://yusuke.homeip.net/twitter4j/custom_callback");
    http = new HttpClient();

    System.out.println("AuthorizationURL: " + 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));
    //        response = http.post(authorizeURL, params);
    resStr = response.asString();
    String oauthVerifier = catchPattern(resStr, "&oauth_verifier=", "\">");

    at = twitter.getOAuthAccessToken(rt.getToken(), rt.getTokenSecret(), oauthVerifier);
    assertEquals(at.getScreenName(), id1.screenName);
    assertEquals(at.getUserId(), 6358482);
  }
Пример #3
0
  public void testSigninWithTwitter() throws Exception {
    RequestToken rt;
    Twitter twitter = new TwitterFactory().getInstance();
    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();

    Map<String, String> props = new HashMap<String, String>();
    response = http.get(rt.getAuthenticationURL());
    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));

    //        response = http.post(authorizeURL, params);
    at = twitter.getOAuthAccessToken(rt.getToken(), rt.getTokenSecret());
    assertEquals(at.getScreenName(), id1.screenName);
    assertEquals(at.getUserId(), 6358482);
  }