Example usage for twitter4j.auth OAuthAuthorization OAuthAuthorization

List of usage examples for twitter4j.auth OAuthAuthorization OAuthAuthorization

Introduction

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

Prototype

public OAuthAuthorization(Configuration conf) 

Source Link

Usage

From source file:mx.bigdata.t4j.TwitterStreamFactory.java

License:Apache License

/**
 * Returns a OAuth Authenticated instance.<br>
 * consumer key and consumer Secret must be provided by twitter4j.properties,
 * or system properties.//from w w w .java  2s .  c om
 * Unlike {@link TwitterStream#setOAuthAccessToken(twitter4j.auth.AccessToken)}, 
 * this factory method potentially returns a cached instance.
 *
 * @param accessToken access token
 * @return an instance
 */
public TwitterStream getInstance(AccessToken accessToken, StreamListener listener) {
    String consumerKey = conf.getOAuthConsumerKey();
    String consumerSecret = conf.getOAuthConsumerSecret();
    if (null == consumerKey && null == consumerSecret) {
        throw new IllegalStateException("Consumer key and Consumer secret not supplied.");
    }
    OAuthAuthorization oauth = new OAuthAuthorization(conf);
    oauth.setOAuthAccessToken(accessToken);
    return getInstance(conf, oauth, listener);
}

From source file:mx.bigdata.t4j.TwitterStreamImpl.java

License:Apache License

private void init() {
    if (null == auth) {
        // try to populate OAuthAuthorization if available in the configuration
        String consumerKey = conf.getOAuthConsumerKey();
        String consumerSecret = conf.getOAuthConsumerSecret();
        // try to find oauth tokens in the configuration
        if (null != consumerKey && null != consumerSecret) {
            OAuthAuthorization oauth = new OAuthAuthorization(conf);
            String accessToken = conf.getOAuthAccessToken();
            String accessTokenSecret = conf.getOAuthAccessTokenSecret();
            if (null != accessToken && null != accessTokenSecret) {
                oauth.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));
            }/*from   w  w  w  .  jav  a 2 s.com*/
            this.auth = oauth;
        } else {
            this.auth = NullAuthorization.getInstance();
        }
    }
}

From source file:org.craftercms.cstudio.publishing.processor.TwitterPostProcessor.java

License:Open Source License

/**
 * factory method for twitter upload instance. Override if you want to authenticate differently
 * @return twitter uploader instance/*from ww w  .  ja v a 2s.  co m*/
 */
protected ImageUpload getTwitterUploaderInstance() {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setDebugEnabled(true).setOAuthConsumerKey(consumerKeyStr).setOAuthConsumerSecret(consumerSecretStr)
            .setOAuthAccessToken(accessTokenStr).setOAuthAccessTokenSecret(accessTokenSecretStr);

    Configuration configuration = builder.build();
    OAuthAuthorization auth = new OAuthAuthorization(configuration);
    ImageUpload uploader = new ImageUploadFactory(configuration).getInstance(auth);

    return uploader;
}

From source file:org.getlantern.firetweet.util.Utils.java

License:Open Source License

public static Authorization getTwitterAuthorization(final Context context,
        final ParcelableCredentials account) {
    if (context == null || account == null)
        return null;
    switch (account.auth_type) {
    case Accounts.AUTH_TYPE_OAUTH:
    case Accounts.AUTH_TYPE_XAUTH: {
        final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
                Context.MODE_PRIVATE);
        // Here I use old consumer key/secret because it's default
        // key for older
        // versions
        final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY);
        final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET);
        final ConfigurationBuilder cb = new ConfigurationBuilder();
        if (!isEmpty(account.api_url_format)) {
            final String versionSuffix = account.no_version_suffix ? null : "/1.1/";
            cb.setRestBaseURL(getApiUrl(account.api_url_format, "api", versionSuffix));
            cb.setOAuthBaseURL(getApiUrl(account.api_url_format, "api", "/oauth/"));
            cb.setUploadBaseURL(getApiUrl(account.api_url_format, "upload", versionSuffix));
            cb.setOAuthAuthorizationURL(getApiUrl(account.api_url_format, null, null));
            if (!account.same_oauth_signing_url) {
                cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL);
                cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL);
                cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL);
            }/*  w  w w  .  j  a  v a2 s .  c o m*/
        }
        if (!isEmpty(account.consumer_key) && !isEmpty(account.consumer_secret)) {
            cb.setOAuthConsumerKey(account.consumer_key);
            cb.setOAuthConsumerSecret(account.consumer_secret);
        } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) {
            cb.setOAuthConsumerKey(prefConsumerKey);
            cb.setOAuthConsumerSecret(prefConsumerSecret);
        } else {
            cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
            cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
        }
        final OAuthAuthorization auth = new OAuthAuthorization(cb.build());
        auth.setOAuthAccessToken(new AccessToken(account.oauth_token, account.oauth_token_secret));
        return auth;
    }
    case Accounts.AUTH_TYPE_BASIC: {
        final String screenName = account.screen_name;
        final String username = account.basic_auth_username;
        final String loginName = username != null ? username : screenName;
        final String password = account.basic_auth_password;
        if (isEmpty(loginName) || isEmpty(password))
            return null;
        return new BasicAuthorization(loginName, password);
    }
    default: {
        return null;
    }
    }
}

From source file:org.getlantern.firetweet.util.Utils.java

License:Open Source License

public static Authorization getTwitterAuthorization(final Context context, final long accountId) {

    final String where = Expression.equals(new Column(Accounts.ACCOUNT_ID), accountId).getSQL();
    final Cursor c = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI,
            Accounts.COLUMNS, where, null, null);
    if (c == null)
        return null;
    try {/* w w w  .  j  ava2s .c om*/
        if (!c.moveToFirst())
            return null;

        switch (c.getInt(c.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) {
        case Accounts.AUTH_TYPE_OAUTH:
        case Accounts.AUTH_TYPE_XAUTH: {
            final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME,
                    Context.MODE_PRIVATE);
            // Here I use old consumer key/secret because it's default
            // key for older
            // versions
            final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY);
            final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET);
            final ConfigurationBuilder cb = new ConfigurationBuilder();
            final String apiUrlFormat = c.getString(c.getColumnIndex(Accounts.API_URL_FORMAT));
            final String consumerKey = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_KEY)));
            final String consumerSecret = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_SECRET)));
            final boolean sameOAuthSigningUrl = c
                    .getInt(c.getColumnIndex(Accounts.SAME_OAUTH_SIGNING_URL)) == 1;
            if (!isEmpty(apiUrlFormat)) {
                cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", "/1.1/"));
                cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/"));
                cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", "/1.1/"));
                cb.setOAuthAuthorizationURL(getApiUrl(apiUrlFormat, null, null));
                if (!sameOAuthSigningUrl) {
                    cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL);
                    cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL);
                    cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL);
                }
            }
            if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) {
                cb.setOAuthConsumerKey(consumerKey);
                cb.setOAuthConsumerSecret(consumerSecret);
            } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) {
                cb.setOAuthConsumerKey(prefConsumerKey);
                cb.setOAuthConsumerSecret(prefConsumerSecret);
            } else {
                cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
                cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
            }
            final OAuthAuthorization auth = new OAuthAuthorization(cb.build());
            final String token = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN));
            final String tokenSecret = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN_SECRET));
            auth.setOAuthAccessToken(new AccessToken(token, tokenSecret));
            return auth;
        }
        case Accounts.AUTH_TYPE_BASIC: {
            final String screenName = c.getString(c.getColumnIndexOrThrow(Accounts.SCREEN_NAME));
            final String username = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_USERNAME));
            final String loginName = username != null ? username : screenName;
            final String password = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD));
            if (isEmpty(loginName) || isEmpty(password))
                return null;
            return new BasicAuthorization(loginName, password);
        }
        default: {
            return null;
        }
        }
    } finally {
        c.close();
    }
}

From source file:org.talend.spark.operation.TwitterLoad.java

License:Open Source License

public static JavaDStream<List<Object>> twitterStream(JavaStreamingContext ctx, String username,
        String password, String accessToken, String secretToken, String[] filters,
        List<TwitterParameter> twitterParameters) {
    twitter4j.conf.ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthAccessToken(accessToken);
    builder.setOAuthAccessTokenSecret(secretToken);
    builder.setOAuthConsumerKey(username);
    builder.setOAuthConsumerSecret(password);
    JavaDStream<Status> inputDStream = null;
    if (filters.length > 0) {
        if (filters.length == 1 && filters[0].equals(""))
            inputDStream = TwitterUtils.createStream(ctx, new OAuthAuthorization(builder.build()));
        else/*  w  ww  . j  a  v  a  2 s.c om*/
            inputDStream = TwitterUtils.createStream(ctx, new OAuthAuthorization(builder.build()), filters);

        return inputDStream.map(new LoadTwitterFunction(twitterParameters));
    }
    return null;
}