Example usage for twitter4j.auth AccessToken AccessToken

List of usage examples for twitter4j.auth AccessToken AccessToken

Introduction

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

Prototype

public AccessToken(String token, String tokenSecret) 

Source Link

Usage

From source file:com.alta189.cyborg.commandkit.twitter.TwitterUser.java

License:Open Source License

public AccessToken getAccessTokenObject() {
    return new AccessToken(accessToken, accessTokenSecret);
}

From source file:com.amazonbird.announce.AnnouncerMgrImpl.java

License:Open Source License

public boolean announcerIsvalid(Announcer announcer) {
    try {//from w  ww.  j a va2  s  .c  o  m
        Twitter twitter = new TwitterFactory().getInstance();
        twitter.setOAuthConsumer(announcer.getConsumerKey(), announcer.getConsumerSecret());
        twitter.setOAuthAccessToken(
                new AccessToken(announcer.getAccessToken(), announcer.getAccessTokenSecret()));

    } catch (Exception ex) {
        return false;
    }
    return true;

}

From source file:com.amazonbird.db.data.Announcer.java

License:Open Source License

public Twitter getTwitterProxy() throws TwitterException {
    if (twitter == null) {

        TwitterFactory tf = new TwitterFactory();
        twitter = tf.getInstance();//w  ww . ja v  a  2s.co m

        twitter.setOAuthConsumer(getConsumerKey(), getConsumerSecret());
        twitter.setOAuthAccessToken(new AccessToken(getAccessToken(), getAccessTokenSecret()));
    }
    return twitter;
}

From source file:com.cafeform.iumfs.twitterfs.TwitterFactoryAdapter.java

License:Apache License

public static AccessToken getAccessToken(String username) {
    AccessToken accessToken = null;/*from   ww w.jav  a2  s  .  c o  m*/
    if (Prefs.get(username + "/accessToken").isEmpty()) {
        Twitter twitter = factory.getInstance();
        twitter.setOAuthConsumer(Prefs.get("OAuthConsumerKey"), Prefs.get("consumerSecret"));
        RequestToken requestToken;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            requestToken = twitter.getOAuthRequestToken();
            while (null == accessToken) {
                System.out.println("Open the following URL and grant access to your account:");
                System.out.println(requestToken.getAuthorizationURL());
                System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
                String pin = br.readLine();
                try {
                    if (pin.length() > 0) {
                        accessToken = twitter.getOAuthAccessToken(requestToken, pin);
                    } else {
                        accessToken = twitter.getOAuthAccessToken();
                    }
                } catch (TwitterException ex) {
                    if (401 == ex.getStatusCode()) {
                        System.out.println("Unable to get the access token.");
                    } else {
                        logger.log(Level.SEVERE, "Unable to get the Access Token", ex);
                    }
                }
            }
        } catch (IOException | TwitterException ex) {
            logger.log(Level.SEVERE, "Unable to get the Access Token", ex);
        }
        Prefs.put(username + "/accessToken", accessToken.getToken());
        Prefs.put(username + "/accessTokenSecret", accessToken.getTokenSecret());
    }
    logger.finest("Token&Secret: " + Prefs.get(username + "/accessToken") + " "
            + Prefs.get(username + "/accessTokenSecret"));
    logger.finest("OauthConsum&Secret: " + Prefs.get("OAuthConsumerKey") + " " + Prefs.get("consumerSecret"));

    accessToken = new AccessToken(Prefs.get(username + "/accessToken"),
            Prefs.get(username + "/accessTokenSecret"));
    return accessToken;
}

From source file:com.codegoogle.twitterandroid.TwitterApp.java

License:Apache License

public void processToken(String callbackUrl) {
    progressDialog.setMessage("Finalizing ...");
    progressDialog.show();//from   w w  w.j  av  a 2  s. com

    final String verifier = getVerifier(callbackUrl);

    new Thread() {
        @Override
        public void run() {
            int what = 1;

            try {
                httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);

                accessToken = new AccessToken(httpOauthConsumer.getToken(), httpOauthConsumer.getTokenSecret());

                configureToken();

                User user = twitter.verifyCredentials();

                session.storeAccessToken(accessToken, user.getName());

                what = 0;
            } catch (Exception e) {
                e.printStackTrace();
            }

            handler.sendMessage(handler.obtainMessage(what, 2, 0));
        }
    }.start();
}

From source file:com.codegoogle.twitterandroid.TwitterSession.java

License:Apache License

public AccessToken getAccessToken() {
    String token = pref.getString(TWEET_AUTH_KEY, null);
    String tokenSecret = pref.getString(TWEET_AUTH_SECRET_KEY, null);

    if (token != null && tokenSecret != null) {
        return new AccessToken(token, tokenSecret);
    } else {// w ww.j ava2s. co m
        return null;
    }
}

From source file:com.company.TwitterPopularLinks.java

License:Apache License

public void sparkStreaming(SparkConf conf) {

    ///Creates Streaming Context
    JavaStreamingContext jsc = new JavaStreamingContext(conf, Durations.seconds(1));

    //Create a Twitter
    Twitter twitter = new TwitterFactory().getInstance();
    twitter.setOAuthConsumer(consumerKey, consumerSecret);
    twitter.setOAuthAccessToken(new AccessToken(accessTokenKey, accessTokenKey_secret));

    JavaDStream<Status> stream = TwitterUtils.createStream(jsc, twitter.getAuthorization());

    JavaDStream<String> words = stream.map(new Function<Status, String>() {
        public String call(Status status) {
            return status.getText();
        }//from   w w w . ja v a2 s . c o m
    });

    JavaDStream<String> statuses = words.flatMap(new FlatMapFunction<String, String>() {
        public Iterable<String> call(String in) {
            return Arrays.asList(in.split(" "));
        }
    });
    //Get the stream of hashtags from the stream of tweets
    JavaDStream<String> hashTags = statuses.filter(new Function<String, Boolean>() {
        public Boolean call(String word) {
            return word.startsWith("#");
        }
    });
    //Count the hashtags over a 5 minute window
    JavaPairDStream<String, Integer> tuples = hashTags.mapToPair(new PairFunction<String, String, Integer>() {
        public Tuple2<String, Integer> call(String in) {
            return new Tuple2<String, Integer>(in, 1);
        }
    });
    //count these hashtags over a 5 minute moving window
    JavaPairDStream<String, Integer> counts = tuples
            .reduceByKeyAndWindow(new Function2<Integer, Integer, Integer>() {
                public Integer call(Integer i1, Integer i2) {
                    return i1 + i2;
                }
            }, new Function2<Integer, Integer, Integer>() {
                public Integer call(Integer i1, Integer i2) {
                    return i1 - i2;
                }
            }, new Duration(60 * 5 * 1000), new Duration(1 * 1000));

    counts.print();
    //jsc.checkpoint(checkPoint);
    jsc.start();
    jsc.awaitTermination();

}

From source file:com.djbrick.twitter_photo_uploader.MSTwitter.java

License:Apache License

/**
 * Get the access token stored in the shared preferences file
 * @param context A contect that has access to the shared file
 * @return/* w w w  .  j a  va2 s  . com*/
 */
protected static AccessToken getAccessToken(Context context) {
    // Create shared preference object to remember if the user has already given us permission
    SharedPreferences prefs = context.getSharedPreferences(PERF_FILENAME, Context.MODE_PRIVATE);

    // do we already have an access credentials saved from a previous tweet?
    if (prefs.contains(PREF_ACCESS_TOKEN) && prefs.contains(PREF_ACCESS_TOKEN_SECRET)) {
        // set access token using saved token and secret values
        String token = prefs.getString(PREF_ACCESS_TOKEN, null);
        String secret = prefs.getString(PREF_ACCESS_TOKEN_SECRET, null);
        return new AccessToken(token, secret);
    } else {
        return null;
    }
}

From source file:com.dwdesign.tweetings.util.Utils.java

License:Open Source License

public static AccessToken getTwitterAccessToken(final Context context, final long account_id) {
    if (context == null)
        return null;
    AccessToken accessToken = null;//from w  w w .j  a v a  2  s . c  om

    final StringBuilder where = new StringBuilder();
    where.append(Accounts.USER_ID + "=" + account_id);

    final Cursor cur = context.getContentResolver().query(Accounts.CONTENT_URI, Accounts.COLUMNS,
            where.toString(), null, null);
    if (cur != null) {
        if (cur.getCount() == 1) {
            cur.moveToFirst();
            accessToken = new AccessToken(cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN)),
                    cur.getString(cur.getColumnIndexOrThrow(Accounts.TOKEN_SECRET)));
        }
        cur.close();
    }
    return accessToken;
}

From source file:com.dwdesign.tweetings.util.Utils.java

License:Open Source License

public static Twitter getTwitterInstance(final Context context, final long account_id,
        final boolean include_entities, final boolean include_rts, final boolean use_httpclient) {
    if (context == null)
        return null;
    final SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
            Context.MODE_PRIVATE);
    final boolean enable_gzip_compressing = preferences != null
            ? preferences.getBoolean(PREFERENCE_KEY_GZIP_COMPRESSING, true)
            : true;/*from   w ww. j  a  v  a  2  s.c om*/
    final boolean ignore_ssl_error = preferences != null
            ? preferences.getBoolean(PREFERENCE_KEY_IGNORE_SSL_ERROR, false)
            : false;
    final boolean enable_proxy = preferences != null
            ? preferences.getBoolean(PREFERENCE_KEY_ENABLE_PROXY, false)
            : false;
    final String consumer_key = preferences != null
            ? preferences.getString(PREFERENCE_KEY_CONSUMER_KEY, CONSUMER_KEY)
            : CONSUMER_KEY;
    final String consumer_secret = preferences != null
            ? preferences.getString(PREFERENCE_KEY_CONSUMER_SECRET, CONSUMER_SECRET)
            : CONSUMER_SECRET;

    Twitter twitter = null;
    final StringBuilder where = new StringBuilder();
    where.append(Accounts.USER_ID + "=" + account_id);
    final Cursor cur = context.getContentResolver().query(Accounts.CONTENT_URI, Accounts.COLUMNS,
            where.toString(), null, null);
    if (cur != null) {
        if (cur.getCount() == 1) {
            cur.moveToFirst();
            final ConfigurationBuilder cb = new ConfigurationBuilder();
            setUserAgent(context, cb);
            if (use_httpclient) {
                cb.setHttpClientImplementation(HttpClientImpl.class);
            }
            cb.setGZIPEnabled(enable_gzip_compressing);
            if (enable_proxy) {
                final String proxy_host = preferences.getString(PREFERENCE_KEY_PROXY_HOST, null);
                final int proxy_port = parseInt(preferences.getString(PREFERENCE_KEY_PROXY_PORT, "-1"));
                if (!isNullOrEmpty(proxy_host) && proxy_port > 0) {
                    cb.setHttpProxyHost(proxy_host);
                    cb.setHttpProxyPort(proxy_port);
                }

            }
            final String rest_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.REST_BASE_URL));
            final String signing_rest_base_url = cur
                    .getString(cur.getColumnIndexOrThrow(Accounts.SIGNING_REST_BASE_URL));
            final String search_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.SEARCH_BASE_URL));
            final String upload_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.UPLOAD_BASE_URL));
            final String oauth_base_url = cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_BASE_URL));
            final String signing_oauth_base_url = cur
                    .getString(cur.getColumnIndexOrThrow(Accounts.SIGNING_OAUTH_BASE_URL));
            //if (!isNullOrEmpty(rest_base_url)) {
            cb.setRestBaseURL(DEFAULT_REST_BASE_URL);
            //}
            //if (!isNullOrEmpty(search_base_url)) {
            cb.setSearchBaseURL(DEFAULT_SEARCH_BASE_URL);
            //}
            cb.setIncludeEntitiesEnabled(include_entities);
            cb.setIncludeRTsEnabled(include_rts);

            switch (cur.getInt(cur.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) {
            case Accounts.AUTH_TYPE_OAUTH:
            case Accounts.AUTH_TYPE_XAUTH:
                if (isNullOrEmpty(consumer_key) || isNullOrEmpty(consumer_secret)) {
                    cb.setOAuthConsumerKey(CONSUMER_KEY);
                    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
                } else {
                    cb.setOAuthConsumerKey(consumer_key);
                    cb.setOAuthConsumerSecret(consumer_secret);
                }
                twitter = new TwitterFactory(cb.build()).getInstance(
                        new AccessToken(cur.getString(cur.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN)),
                                cur.getString(cur.getColumnIndexOrThrow(Accounts.TOKEN_SECRET))));
                break;
            case Accounts.AUTH_TYPE_BASIC:
                twitter = new TwitterFactory(cb.build()).getInstance(
                        new BasicAuthorization(cur.getString(cur.getColumnIndexOrThrow(Accounts.USERNAME)),
                                cur.getString(cur.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD))));
                break;
            default:
            }
        }
        cur.close();
    }
    return twitter;
}