Example usage for twitter4j.auth OAuthAuthorization setOAuthConsumer

List of usage examples for twitter4j.auth OAuthAuthorization setOAuthConsumer

Introduction

In this page you can find the example usage for twitter4j.auth OAuthAuthorization setOAuthConsumer.

Prototype

@Override
    public void setOAuthConsumer(String consumerKey, String consumerSecret) 

Source Link

Usage

From source file:com.mastfrog.acteur.twitter.TwitterSign.java

@SuppressWarnings("unchecked")
RemoteUserInfo zgetUserInfo(String oauth_nonce, TwitterOAuthPlugin.TwitterToken credential,
        AuthorizationResponse auth)/* w  w  w .j a v a2  s .  c  o  m*/
        throws UnsupportedEncodingException, GeneralSecurityException, InterruptedException, IOException {
    System.setProperty("twitter4j.http.useSSL", "false");

    Twitter twitter = TwitterFactory.getSingleton();
    try {
        // Idiotic - shutdown does not clear state
        twitter.setOAuthConsumer(twitter_consumer_key, twitter_consumer_secret);
    } catch (Exception e) {
        e.printStackTrace();
    }
    twitter.setOAuthAccessToken(new AccessToken(auth.accessToken, auth.accessTokenSecret));
    OAuthAuthorization a = new OAuthAuthorization(twitter.getConfiguration());
    a.setOAuthConsumer(twitter_consumer_key, twitter_consumer_secret);

    URL url = URL.builder(Protocols.HTTPS)
            //        URL url = URL.builder(Protocols.HTTP) // XXX NOT UNENCRYPTED!  JUST FOR DEBUGING
            .setHost(Host.parse("api.twitter.com")).setPath("1.1/account/verify_credentials.json").create();

    String hdr = new SigBuilder().add(oauth_token, auth.accessToken).add(OAuthHeaders.oauth_nonce, oauth_nonce)
            .toHeader(Method.GET, url.getPathAndQuery(), auth); // XXX encode URL?

    String franken = a.xgenerateAuthorizationHeader(oauth_nonce, "GET", "/1.1/account/verify_credentials.json",
            new HttpParameter[0], new twitter4j.auth.AccessToken(auth.accessToken, auth.accessTokenSecret));

    OAuthAuthorization oa = new OAuthAuthorization(twitter.getConfiguration());
    oa.setOAuthConsumer(twitter_consumer_key, twitter_consumer_secret);
    oa.setOAuthAccessToken(new AccessToken(auth.accessToken, auth.accessTokenSecret));

    twitter4j.internal.http.HttpRequest r = new twitter4j.internal.http.HttpRequest(RequestMethod.GET,
            "https://api.twitter.com/twitter4j.internal.http.HttpRequest", new HttpParameter[0], oa,
            Collections.<String, String>emptyMap());

    ResponseLatch latch = new ResponseLatch();
    RH rh = new RH();
    client.get().setURL(url).addHeader(Headers.stringHeader("Authorization"), hdr)
            .addHeader(Headers.stringHeader("X-Twitter-Client-URL"),
                    "http://twitter4j.org/en/twitter4j-3.0.4-SNAPSHOT.xml")
            .addHeader(Headers.stringHeader("X-Twitter-Client"), "Twitter4J")
            .addHeader(Headers.stringHeader("Accept-Encoding"), "gzip")
            .addHeader(Headers.stringHeader("X-Twitter-Client-Version"), "3.0.4-SNAPSHOT")
            .addHeader(Headers.stringHeader("Accept"), "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
            //                .addHeader(Headers.stringHeader("Accept"), "*/*")
            //                .addHeader(Headers.stringHeader("Connection"), "keep-alive")
            .noDateHeader()
            //                .noConnectionHeader()
            //                .addHeader(Headers.stringHeader("Content-Type"), "application/x-www-form-urlencoded")
            //                .setBody("screen_name=kablosna", MediaType.PLAIN_TEXT_UTF_8)
            .on(StateType.Closed, latch).on(StateType.Timeout, latch).execute(rh);
    rh.await(1, TimeUnit.MINUTES);
    latch.latch.await(1, TimeUnit.MINUTES);

    String responseBody = rh.getResponse();

    RUI rui = new RUI();
    if (responseBody == null) {
        //            System.out.println("NULL RESPONSE BODY.");
        throw new IOException(rh.toString());
    }
    rui.putAll(new ObjectMapper().readValue(responseBody, Map.class));
    return rui;
}

From source file:hashimotonet.UpdateStatus.java

License:Apache License

/**
 * Twitter?// w  w  w . ja  va  2 s . c  om
 * @throws TwitterException 
 */
private void connectTwitter() throws TwitterException {
    // Twitetr4j??
    Configuration conf = ConfigurationContext.getInstance();
    // OauthF?IuWFNg??
    OAuthAuthorization Oauth = new OAuthAuthorization(conf);
    // OauthF?IuWFNgconsumerKeyconsumerSecret?
    Oauth.setOAuthConsumer(Globals.KEY, Globals.SECRET);

    //       // NGXgg?[N??
    RequestToken sRequestToken = null;
    try {
        sRequestToken = Oauth.getOAuthRequestToken();
    } catch (TwitterException e) {
        throw new TwitterException(e.toString());
    }
    String url = sRequestToken.getAuthorizationURL();
    System.out.println("Url = " + url);
}