Search in sources :

Example 31 with TwitterFactory

use of twitter4j.TwitterFactory in project twicalico by moko256.

the class GlobalApplication method getTwitterInstance.

@NonNull
public Twitter getTwitterInstance(@NonNull AccessToken accessToken) {
    Twitter t;
    Configuration conf;
    if (accessToken.getType() == Type.TWITTER) {
        conf = new ConfigurationBuilder().setTweetModeExtended(true).setOAuthConsumerKey(BuildConfig.CONSUMER_KEY).setOAuthConsumerSecret(BuildConfig.CONSUMER_SECRET).setOAuthAccessToken(accessToken.getToken()).setOAuthAccessTokenSecret(accessToken.getTokenSecret()).build();
        t = twitterCache.get(conf);
        if (t == null) {
            t = new TwitterFactory(conf).getInstance();
            twitterCache.put(conf, t);
        }
    } else {
        conf = new ConfigurationBuilder().setOAuthAccessToken(accessToken.getToken()).setRestBaseURL(accessToken.getUrl()).build();
        t = twitterCache.get(conf);
        if (t == null) {
            t = new MastodonTwitterImpl(conf, accessToken.getUserId(), getOkHttpClient(conf.getHttpClientConfiguration()).newBuilder());
            twitterCache.put(conf, t);
        }
    }
    return t;
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) AppConfiguration(com.github.moko256.twicalico.config.AppConfiguration) HttpClientConfiguration(twitter4j.HttpClientConfiguration) Configuration(twitter4j.conf.Configuration) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) MastodonTwitterImpl(com.github.moko256.mastodon.MastodonTwitterImpl) NonNull(android.support.annotation.NonNull)

Example 32 with TwitterFactory

use of twitter4j.TwitterFactory in project AndroidSDK-RecipeBook by gabu.

the class Auth method onOKButton.

// OKボタンが押されたら呼び出される
public void onOKButton(View view) {
    // 入力されたユーザIDとパスワードを取得
    String userID = ((EditText) findViewById(R.id.user_id)).getText().toString();
    String password = ((EditText) findViewById(R.id.password)).getText().toString();
    // とりあえずユーザIDとパスワードでTwitterインスタンスを生成
    Twitter twitter = new TwitterFactory().getInstance(userID, password);
    try {
        // AccessTokenを取得
        AccessToken accessToken = twitter.getOAuthAccessToken();
        // tokenとtokenSecretを取得
        String token = accessToken.getToken();
        String tokenSecret = accessToken.getTokenSecret();
        // プリファレンスのEditorを取得
        Editor e = getSharedPreferences(Recipe102.PREF_NAME, MODE_PRIVATE).edit();
        // tokenとtokenSecretを書き込んで
        e.putString(Recipe102.PREF_KEY_TOKEN, token);
        e.putString(Recipe102.PREF_KEY_TOKEN_SECRET, tokenSecret);
        // 保存!
        e.commit();
        // Authアクティビティを終了
        finish();
    } catch (TwitterException e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "ユーザIDかパスワードが間違っています。", Toast.LENGTH_SHORT).show();
    }
}
Also used : AccessToken(twitter4j.http.AccessToken) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) Editor(android.content.SharedPreferences.Editor) TwitterException(twitter4j.TwitterException)

Example 33 with TwitterFactory

use of twitter4j.TwitterFactory in project AndroidSDK-RecipeBook by gabu.

the class Recipe102 method onResume.

public void onResume() {
    super.onResume();
    // プリファレンスを取得
    SharedPreferences sp = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    // tokenとtokenSecretを取得
    String token = sp.getString(PREF_KEY_TOKEN, "");
    String tokenSecret = sp.getString(PREF_KEY_TOKEN_SECRET, "");
    // 値がなければAuthアクティビティを起動
    if ("".equals(token) || "".equals(tokenSecret)) {
        Intent intent = new Intent(this, Auth.class);
        startActivity(intent);
    }
    // twitter4jのConfigurationを取得
    Configuration conf = ConfigurationContext.getInstance();
    // AccessTokenを生成
    AccessToken accessToken = new AccessToken(token, tokenSecret);
    // OAuthAuthorizationを生成
    Authorization auth = new OAuthAuthorization(conf, conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(), accessToken);
    // OAuthAuthorizationを使ってTwitterインスタンスを生成
    Twitter twitter = new TwitterFactory().getInstance(auth);
    try {
        // とりあえずテストのためTLをログ出力
        ResponseList<Status> statuses = twitter.getHomeTimeline();
        for (Status status : statuses) {
            Log.d(TAG, status.getUser().getName() + ":" + status.getText());
        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}
Also used : Authorization(twitter4j.http.Authorization) OAuthAuthorization(twitter4j.http.OAuthAuthorization) Status(twitter4j.Status) Configuration(twitter4j.conf.Configuration) SharedPreferences(android.content.SharedPreferences) AccessToken(twitter4j.http.AccessToken) Twitter(twitter4j.Twitter) Intent(android.content.Intent) TwitterFactory(twitter4j.TwitterFactory) OAuthAuthorization(twitter4j.http.OAuthAuthorization) TwitterException(twitter4j.TwitterException)

Example 34 with TwitterFactory

use of twitter4j.TwitterFactory in project Talon-for-Twitter by klinker24.

the class Utils method getTwitter.

public static Twitter getTwitter(Context context, AppSettings settings) {
    if (settings == null) {
        settings = AppSettings.getInstance(context);
    }
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey(AppSettings.TWITTER_CONSUMER_KEY).setOAuthConsumerSecret(AppSettings.TWITTER_CONSUMER_SECRET).setOAuthAccessToken(settings.authenticationToken).setOAuthAccessTokenSecret(settings.authenticationTokenSecret);
    cb.setTweetModeExtended(true);
    TwitterFactory tf = new TwitterFactory(cb.build());
    return tf.getInstance();
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) TwitterFactory(twitter4j.TwitterFactory)

Example 35 with TwitterFactory

use of twitter4j.TwitterFactory in project twitter-2-weibo by rjyo.

the class AuthServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpServletRouter r = new HttpServletRouter(request);
    r.setPattern("/:type");
    response.setContentType("text/plain");
    PrintWriter writer = response.getWriter();
    String serverPath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
    HttpSession session = request.getSession();
    if (r.is(":type", "weibo")) {
        try {
            Oauth oauth = new Oauth();
            String redirectUrl = oauth.authorize("code");
            response.setStatus(302);
            response.setHeader("Location", redirectUrl);
            log.info("Redirecting Weibo...");
        } catch (WeiboException e) {
            log.error(e);
        }
    } else if (r.is(":type", "twitter")) {
        log.info("hello world~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter t = factory.getInstance();
            twitter4j.auth.RequestToken requestToken = t.getOAuthRequestToken(serverPath + "/callback/twitter");
            response.setStatus(302);
            log.info(requestToken.getAuthenticationURL());
            response.setHeader("Location", requestToken.getAuthenticationURL());
            session.setAttribute(Keys.SESSION_REQUEST_TOKEN, requestToken);
            log.info("Redirecting Twitter...");
        } catch (TwitterException e) {
            log.error(e);
        }
        writer.close();
    } else {
        response.setStatus(200);
        writer.println("Wrong parameter, not working!");
        writer.close();
    }
}
Also used : Oauth(weibo4j.Oauth) WeiboException(weibo4j.model.WeiboException) HttpSession(javax.servlet.http.HttpSession) HttpServletRouter(h2weibo.HttpServletRouter) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException) PrintWriter(java.io.PrintWriter)

Aggregations

TwitterFactory (twitter4j.TwitterFactory)68 Twitter (twitter4j.Twitter)60 TwitterException (twitter4j.TwitterException)55 ConfigurationBuilder (twitter4j.conf.ConfigurationBuilder)10 Status (twitter4j.Status)7 IDs (twitter4j.IDs)6 AccessToken (twitter4j.auth.AccessToken)5 SavedSearch (twitter4j.SavedSearch)4 RequestToken (twitter4j.auth.RequestToken)4 Configuration (twitter4j.conf.Configuration)4 File (java.io.File)3 User (twitter4j.User)3 UserList (twitter4j.UserList)3 Intent (android.content.Intent)2 AppSettings (com.klinker.android.twitter.settings.AppSettings)2 HttpServletRouter (h2weibo.HttpServletRouter)2 HashMap (java.util.HashMap)2 HttpSession (javax.servlet.http.HttpSession)2 AccessToken (twitter4j.http.AccessToken)2 AlarmManager (android.app.AlarmManager)1