Example usage for twitter4j TwitterFactory TwitterFactory

List of usage examples for twitter4j TwitterFactory TwitterFactory

Introduction

In this page you can find the example usage for twitter4j TwitterFactory TwitterFactory.

Prototype

public TwitterFactory() 

Source Link

Document

Creates a TwitterFactory with the root configuration.

Usage

From source file:cmu.edu.homework.mediaUpload.MiMp4Upload.java

License:Apache License

@Override
protected void preUpload() throws TwitterException {
    uploadUrl = "https://upload.twitter.com/1.1/media/upload.json";
    String signedVerifyCredentialsURL = generateVerifyCredentialsAuthorizationURL(
            "https://api.twitter.com/1.1/account/verify_credentials.xml");
    Twitter tw = new TwitterFactory().getInstance(this.oauth);

    //        HttpParameter[] params = {
    //                new HttpParameter("auth", "oauth"),
    //                new HttpParameter("username", tw.verifyCredentials().getScreenName()),
    //                new HttpParameter("verify_url", signedVerifyCredentialsURL),
    //        };/*from  w w w .j  a va  2s.  c om*/
    //        if (message != null) {
    //            params = appendHttpParameters(new HttpParameter[]{
    //                    this.message
    //            }, params);
    //        }
    //        this.postParameter = params;
}

From source file:com.adisayoga.earthquake.wrapper.EarthquakeTwitter.java

License:Apache License

public EarthquakeTwitter(Context context) {
    this.context = context;
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

    prefs = Prefs.getInstance(context);/*from   w w  w. j a  v  a  2  s  .  c  o  m*/
}

From source file:com.adisayoga.earthquake.wrapper.TwitterLoginActivity.java

License:Apache License

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);// w ww . j  av a 2  s. c  o m

    prefs = Prefs.getInstance(this);
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(EarthquakeTwitter.CONSUMER_KEY, EarthquakeTwitter.CONSUMER_SECRET);

    BrowserActivityClient client = new BrowserActivityClient(CALLBACK_URL);
    client.setCallbackListener(new BrowserActivityClient.CallbackListener() {
        @Override
        public void onRedirect(Uri uri) {
            dealWithResponse(uri);
        }
    });

    webView = (WebView) findViewById(R.id.webview);
    webView.setWebViewClient(client);
    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int progress) {
            setProgress(progress * 100);
            if (progress == 100) {
                setProgressBarIndeterminateVisibility(false);
                setProgressBarVisibility(false);
            }
        }
    });
    loginNewUser();
}

From source file:com.ak.android.akplaza.common.sns.twitter.TwitterController.java

License:Open Source License

public static void login(Activity at) {
    twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(C.TWITTER_CONSUMER_KEY, C.TWITTER_CONSUMER_SECRET);
    rqToken = null;//from w w  w. j a v a2  s  .  c  o  m

    try {
        rqToken = twitter.getOAuthRequestToken();

    } catch (TwitterException e) {
        e.printStackTrace();
        rqToken = null;
    }
    //       Log.d("rqToken", rqToken.getToken());

    if (rqToken.equals(null)) {
        //          Log.d("123rqToken", rqToken.getToken());
    } else {
        Intent intent = new Intent(at, TwitterLogin.class);
        intent.putExtra("auth_url", rqToken.getAuthorizationURL());
        at.startActivityForResult(intent, C.TWITTER_LOGIN_CODE);
    }

}

From source file:com.aldebaran.demo.picture.GetAccessToken.java

License:Apache License

/**
 * Usage: java  twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]
 *
 * @param args message/*from  w  w  w  .  j ava 2  s . c  o m*/
 */
public static void main(String[] args) {
    File file = new File("twitter4j.properties");
    Properties prop = new Properties();
    InputStream is = null;
    OutputStream os = null;
    try {
        if (file.exists()) {
            is = new FileInputStream(file);
            prop.load(is);
        }
        if (args.length < 2) {
            if (null == prop.getProperty("oauth.consumerKey")
                    && null == prop.getProperty("oauth.consumerSecret")) {
                // consumer key/secret are not set in twitter4j.properties
                System.out.println(
                        "Usage: java twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]");
                System.exit(-1);
            }
        } else {
            prop.setProperty("oauth.consumerKey", args[0]);
            prop.setProperty("oauth.consumerSecret", args[1]);
            os = new FileOutputStream("twitter4j.properties");
            prop.store(os, "twitter4j.properties");
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(-1);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        RequestToken requestToken = twitter.getOAuthRequestToken();
        System.out.println("Got request token.");
        System.out.println("Request token: " + requestToken.getToken());
        System.out.println("Request token secret: " + requestToken.getTokenSecret());
        AccessToken accessToken = null;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (null == accessToken) {
            System.out.println("Open the following URL and grant access to your account:");
            System.out.println(requestToken.getAuthorizationURL());
            try {
                Desktop.getDesktop().browse(new URI(requestToken.getAuthorizationURL()));
            } catch (UnsupportedOperationException ignore) {
            } catch (IOException ignore) {
            } catch (URISyntaxException e) {
                throw new AssertionError(e);
            }
            System.out.print("Enter the PIN(if available) and hit enter after you granted access.[PIN]:");
            String pin = br.readLine();
            try {
                if (pin.length() > 0) {
                    accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                } else {
                    accessToken = twitter.getOAuthAccessToken(requestToken);
                }
            } catch (TwitterException te) {
                if (401 == te.getStatusCode()) {
                    System.out.println("Unable to get the access token.");
                } else {
                    te.printStackTrace();
                }
            }
        }
        System.out.println("Got access token.");
        System.out.println("Access token: " + accessToken.getToken());
        System.out.println("Access token secret: " + accessToken.getTokenSecret());

        try {
            prop.setProperty("oauth.accessToken", accessToken.getToken());
            prop.setProperty("oauth.accessTokenSecret", accessToken.getTokenSecret());
            os = new FileOutputStream(file);
            prop.store(os, "twitter4j.properties");
            os.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(-1);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ignore) {
                }
            }
        }
        System.out.println("Successfully stored access token to " + file.getAbsolutePath() + ".");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get accessToken: " + te.getMessage());
        System.exit(-1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Failed to read the system input.");
        System.exit(-1);
    }
}

From source file:com.allenzheng.twittersyn.utility.impl.TwitterAPIImpl.java

License:Apache License

    public String getAuthorisationUrl() throws TwitterException{
      Properties prop = loadProperties();
      /*from w w w  .j a va 2  s  . c o  m*/
         Twitter twitter = new TwitterFactory().getInstance();
         twitter.setOAuthConsumer(prop.getProperty("twitter.consumerkey"), 
               prop.getProperty("twitter.consumersecret"));
         try {
            RequestToken requestToken;
            if (callbackUrl == null) {
               requestToken = twitter.getOAuthRequestToken();
            } else {
               requestToken = twitter
                     .getOAuthRequestToken(callbackUrl);
            }
            String authorisationUrl = requestToken
                  .getAuthorizationURL();
//            session.setAttribute(ATTR_TWITTER, twitter);
//            session.setAttribute(ATTR_REQUEST_TOKEN, requestToken);

            logger.debug("Redirecting user to " + authorisationUrl);
//            response.sendRedirect(authorisationUrl);
            return authorisationUrl;
         } catch (TwitterException e) {
            logger.error("Sign in with Twitter failed - "
                  + e.getMessage());
            e.printStackTrace();
            throw new TwitterException(e);
         }
      
      
   }

From source file:com.alta189.deskbin.services.image.TwitterService.java

License:Open Source License

public TwitterService(MediaProvider provider) {
    twitterService = new TwitterFactory().getInstance();
    twitterService.setOAuthConsumer(KeyUtils.getKey("twitter-consumerkey"),
            KeyUtils.getKey("twitter-consumersec"));
    AccessToken token = KeyStore.get("twitter-oauth");
    twitterService.setOAuthAccessToken(token);
}

From source file:com.amandine.twitterpostforcoucou.Tweet.java

private void publishStatusUpdateWithMedia(String message) throws MalformedURLException, IOException {
    Status status = null;//w  w  w  .  ja v  a 2  s  .c  o m
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        try {
            RequestToken requestToken = twitter.getOAuthRequestToken();
            AccessToken accessToken = null;
            while (null == accessToken) {
                logger.fine("Open the following URL and grant access to your account:");
                logger.fine(requestToken.getAuthorizationURL());
                try {
                    accessToken = twitter.getOAuthAccessToken(requestToken);
                } catch (TwitterException te) {
                    if (401 == te.getStatusCode()) {
                        logger.severe("Unable to get the access token.");
                    } else {
                        te.printStackTrace();
                    }
                }
            }
            logger.log(Level.INFO, "Got access token.");
            logger.log(Level.INFO, "Access token: {0}", accessToken.getToken());
            logger.log(Level.INFO, "Access token secret: {0}", accessToken.getTokenSecret());
        } catch (IllegalStateException ie) {
            // access token is already available, or consumer key/secret is not set.
            if (!twitter.getAuthorization().isEnabled()) {
                logger.severe("OAuth consumer key/secret is not set.");
                return;
            }
        }
        //Instantiate and initialize a new twitter status update
        StatusUpdate statusUpdate = new StatusUpdate(message);
        //attach any media, if you want to
        statusUpdate.setMedia(
                //title of media
                "Amandine Leforestier Spring Summer 2015 white",
                new URL("https://issuu.com/kadiemurphy/docs/volume_xxi_issuu/52?e=0").openStream());
        //tweet or update status
        status = twitter.updateStatus(statusUpdate);

        //Status status = twitter.updateStatus(message);
        logger.log(Level.INFO, "Successfully updated the status to [{0}].", status.getText());
    } catch (TwitterException te) {
        te.printStackTrace();
        logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage());
    }
}

From source file:com.amandine.twitterpostforcoucou.Tweet.java

public Twitter getTwitter() {
    Twitter twitter = new TwitterFactory().getInstance();
    try {/*  w w  w .  ja  v a2 s . c  o  m*/
        RequestToken requestToken = twitter.getOAuthRequestToken();
        AccessToken accessToken = null;
        while (null == accessToken) {
            logger.fine("Open the following URL and grant access to your account:");
            logger.fine(requestToken.getAuthorizationURL());
            try {
                accessToken = twitter.getOAuthAccessToken(requestToken);
            } catch (TwitterException te) {
                if (401 == te.getStatusCode()) {
                    logger.severe("Unable to get the access token.");
                } else {
                    logger.log(Level.SEVERE, "Failed to get timeline: {0}", te.getMessage());
                }
            }
        }
        logger.log(Level.INFO, "Got access token.");
        logger.log(Level.INFO, "Access token: {0}", accessToken.getToken());
        logger.log(Level.INFO, "Access token secret: {0}", accessToken.getTokenSecret());
    } catch (IllegalStateException ie) {
        // access token is already available, or consumer key/secret is not set.
        if (!twitter.getAuthorization().isEnabled()) {
            logger.log(Level.SEVERE, "OAuth consumer key/secret is not set: {0}", ie.getMessage());
            return null;
        }
    } catch (TwitterException te) {
        logger.log(Level.SEVERE, "OAuth request token: {0}", te.getMessage());
    }
    return twitter;
}

From source file:com.amandine.twitterpostforcoucou.Tweet.java

public void getFollowersInformation(String FollowersFor) {
    try {//from w w  w  .ja v a  2 s . com
        Twitter twitter = new TwitterFactory().getInstance();
        long cursor = -1;
        IDs ids;
        System.out.println("Listing followers's ids.");
        do {
            //                 if (0 < args.length) { 
            ids = twitter.getFollowersIDs(URLEncoder.encode(FollowersFor), cursor);
            //                 } else { 
            //                     ids = twitter.getFollowersIDs(cursor); 
            //                 } 
            for (long id : ids.getIDs()) {
                //System.out.println(id + " " + twitter.showUser(id).getScreenName());
                //twitter.showUser(id).getDescription language location name
                //twitter.showUser(id).getScreenName() this is rate limited at 180 every 15 minutes
                writeUsersTwitterIdToUserTable(Long.toString(id), "", FollowersFor);
            }
            try {
                Thread.sleep(1000 * 60);
            } catch (InterruptedException ex) {
                logger.log(Level.INFO, "Woke up", ex);
            }
        } while ((cursor = ids.getNextCursor()) != 0);
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get followers' ids: " + te.getMessage());
        System.exit(-1);
    }
}