Search in sources :

Example 11 with TwitterException

use of twitter4j.TwitterException in project android-priority-jobqueue by yigit.

the class PostTweetJob method onAdded.

@Override
public void onAdded() {
    //job has been secured to disk, add item to database
    try {
        Tweet tweet = new Tweet(localId, null, text, TwitterController.getInstance().getUserId(), null, new Date(System.currentTimeMillis()));
        TweetModel.getInstance().insertOrReplace(tweet);
        EventBus.getDefault().post(new PostingTweetEvent(tweet));
    } catch (TwitterException exception) {
    //if we cannot get user id, we won't add it locally for now.
    }
}
Also used : Tweet(com.birbit.android.jobqueue.examples.twitter.entities.Tweet) TwitterException(twitter4j.TwitterException) Date(java.util.Date) PostingTweetEvent(com.birbit.android.jobqueue.examples.twitter.events.PostingTweetEvent)

Example 12 with TwitterException

use of twitter4j.TwitterException in project twitter4j by yusuke.

the class MobypictureUpload method postUpload.

@Override
protected String postUpload() throws TwitterException {
    int statusCode = httpResponse.getStatusCode();
    if (statusCode != 200)
        throw new TwitterException("Mobypic image upload returned invalid status code", httpResponse);
    String response = httpResponse.asString();
    try {
        JSONObject json = new JSONObject(response);
        if (!json.isNull("media")) {
            return json.getJSONObject("media").getString("mediaurl");
        }
    } catch (JSONException e) {
        throw new TwitterException("Invalid Mobypic response: " + response, e);
    }
    throw new TwitterException("Unknown Mobypic response", httpResponse);
}
Also used : JSONObject(twitter4j.JSONObject) JSONException(twitter4j.JSONException) TwitterException(twitter4j.TwitterException)

Example 13 with TwitterException

use of twitter4j.TwitterException in project twitter4j by yusuke.

the class GetAccessToken method main.

/**
     * Usage: java  twitter4j.examples.oauth.GetAccessToken [consumer key] [consumer secret]
     *
     * @param args message
     */
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);
    }
}
Also used : Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) URISyntaxException(java.net.URISyntaxException) Properties(java.util.Properties) URI(java.net.URI) RequestToken(twitter4j.auth.RequestToken) AccessToken(twitter4j.auth.AccessToken) TwitterException(twitter4j.TwitterException)

Example 14 with TwitterException

use of twitter4j.TwitterException in project twitter4j by yusuke.

the class CreateSavedSearch method main.

/**
     * Usage: java twitter4j.examples.savedsearches.CreateSavedSearch [query]
     *
     * @param args message
     */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.savedsearches.CreateSavedSearch [query]");
        System.exit(-1);
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        SavedSearch savedSearch = twitter.createSavedSearch(args[0]);
        System.out.println("Successfully created saved search [name:" + savedSearch.getName() + " query:" + savedSearch.getQuery() + " id:" + savedSearch.getId() + "]");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to create a saved search: " + te.getMessage());
        System.exit(-1);
    }
}
Also used : SavedSearch(twitter4j.SavedSearch) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Example 15 with TwitterException

use of twitter4j.TwitterException in project twitter4j by yusuke.

the class ShowSavedSearch method main.

/**
     * Usage: java twitter4j.examples.savedsearches.ShowSavedSearch [saved search id]
     *
     * @param args message
     */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.savedsearches.ShowSavedSearch [saved search id]");
        System.exit(-1);
    }
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        SavedSearch savedSearch = twitter.showSavedSearch(Integer.parseInt(args[0]));
        System.out.println("[name:" + savedSearch.getName() + " query:" + savedSearch.getQuery() + " id:" + savedSearch.getId() + "]");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get the saved search: " + te.getMessage());
        System.exit(-1);
    }
}
Also used : SavedSearch(twitter4j.SavedSearch) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException)

Aggregations

TwitterException (twitter4j.TwitterException)88 Twitter (twitter4j.Twitter)69 TwitterFactory (twitter4j.TwitterFactory)54 Status (twitter4j.Status)19 Intent (android.content.Intent)9 User (twitter4j.User)8 ArrayList (java.util.ArrayList)7 File (java.io.File)6 DirectMessage (twitter4j.DirectMessage)6 IDs (twitter4j.IDs)6 Paging (twitter4j.Paging)6 Context (android.content.Context)4 AppSettings (com.klinker.android.twitter.settings.AppSettings)4 Date (java.util.Date)4 SavedSearch (twitter4j.SavedSearch)4 AccessToken (twitter4j.auth.AccessToken)4 MentionsDataSource (com.klinker.android.twitter.data.sq_lite.MentionsDataSource)3 IOException (java.io.IOException)3 UserList (twitter4j.UserList)3 RequestToken (twitter4j.auth.RequestToken)3