本文整理了Java中twitter4j.Twitter
类的一些代码示例,展示了Twitter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Twitter
类的具体详情如下:
包路径:twitter4j.Twitter
类名称:Twitter
暂无
代码示例来源:origin: stackoverflow.com
/**
* To upload a picture with some piece of text.
*
*
* @param file The file which we want to share with our tweet
* @param message Message to display with picture
* @param twitter Instance of authorized Twitter class
* @throws Exception exception if any
*/
public void uploadPic(File file, String message,Twitter twitter) throws Exception {
try{
StatusUpdate status = new StatusUpdate(message);
status.setMedia(file);
twitter.updateStatus(status);}
catch(TwitterException e){
Log.d("TAG", "Pic Upload error" + e.getErrorMessage());
throw e;
}
}
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder builder=new ConfigurationBuilder();
builder.setUseSSL(true);
builder.setApplicationOnlyAuthEnabled(true);
// setup
Twitter twitter = new TwitterFactory(builder.build()).getInstance();
// exercise & verify
twitter.setOAuthConsumer(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
// OAuth2Token token = twitter.getOAuth2Token();
twitter.getOAuth2Token();
statuses = twitter.getUserTimeline("Twitter");
代码示例来源:origin: loklak/loklak_server
public static JSONObject getUser(String screen_name, boolean forceReload) throws TwitterException, IOException {
if (!forceReload) {
JsonFactory mapcapsule = DAO.user_dump.get("screen_name",screen_name);
if (mapcapsule == null) mapcapsule = DAO.user_dump.get("id_str", screen_name);
if (mapcapsule != null) {
JSONObject json = mapcapsule.getJSON();
if (json.length() > 0) {
// check if the entry is maybe outdated, i.e. if it is empty or too old
try {
Date d = DAO.user_dump.parseDate(json);
if (d.getTime() + DateParser.DAY_MILLIS > System.currentTimeMillis()) return json;
} catch (ParseException e) {
return json;
}
}
}
}
TwitterFactory tf = getUserTwitterFactory(screen_name);
if (tf == null) tf = getAppTwitterFactory();
if (tf == null) return new JSONObject();
Twitter twitter = tf.getInstance();
User user = twitter.showUser(screen_name);
RateLimitStatus rateLimitStatus = user.getRateLimitStatus();
getUserResetTime = System.currentTimeMillis() + rateLimitStatus.getSecondsUntilReset() * 1000;
getUserRemaining = rateLimitStatus.getRemaining();
JSONObject json = user2json(user);
enrichLocation(json);
DAO.user_dump.putUnique(json);
return json;
}
代码示例来源:origin: google/data-transfer-project
try {
String page = "" + pageNumber;
long id = twitterApi.getId();
monitor.debug(() -> format("Getting tweets for %s (page %s)", id, page));
ResponseList<Status> statuses = twitterApi.getUserTimeline(id, paging);
List<PhotoModel> photos = new ArrayList<>();
for (Status status : statuses) {
boolean hasMedia = status.getMediaEntities().length > 0;
if (hasMedia && !status.isRetweet()) {
for (MediaEntity mediaEntity : status.getMediaEntities()) {
photos.add(
new PhotoModel(
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthAccessToken("ACCESS_TOKEN");
builder.setOAuthAccessTokenSecret("TOKEN_SECRET");
builder.setOAuthConsumerKey("CLIENT_ID");
builder.setOAuthConsumerSecret("CLIENT_SECRET");
OAuthAuthorization auth = new OAuthAuthorization(builder.build());
Twitter twitter = new TwitterFactory().getInstance(auth);
twitter.updateStatus("TEXT TO POST");
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*******")
.setOAuthConsumerSecret("****")
.setOAuthAccessToken("*****")
.setOAuthAccessTokenSecret("*****");
TwitterFactory tf = new TwitterFactory(cb.build());
final Twitter twitter = tf.getInstance();
Query query = new Query("source:twitter4j yusukey");
new Thread(new Runnable() {
@Override
public void run() {
try {
QueryResult result = twitter.search(query);
for (Status status : result.getTweets()) {
System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText());
}
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
代码示例来源:origin: stackoverflow.com
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
AccessToken a = new AccessToken(token, secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(yourConsumerKey).setOAuthConsumerSecret(yourConsumerSeceret).setOAuthAccessToken(yourAccessToken).setOAuthAccessTokenSecret(yourAccessTokenSeceret);
Configuration conf = cb.build();
ImageUploadFactory factory = new ImageUploadFactory(conf);
ImageUpload upload = factory.getInstance();
String url = upload.upload(yourImage, yourCaptionWithImage); //image is a File Format and Caption is String
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(oAuthAccessToken)
.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
twitter.updateStatus("This is a test message"); //ThrowsTwitterException
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));
configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));
configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));
configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));
Configuration configuration = configurationBuilder.build();
final Twitter twitter = new TwitterFactory(configuration).getInstance();
new Thread(new Runnable() {
private double x;
@Override
public void run() {
boolean success = true;
try {
x = Math.random();
twitter.updateStatus(message +" "+x);
} catch (TwitterException e) {
e.printStackTrace();
success = false;
}
final boolean finalSuccess = success;
callingActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
postResponse.onFinsihed(finalSuccess);
}
});
}
}).start();
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("B***************Q")
.setOAuthConsumerSecret(
"l*********************************o")
.setOAuthAccessToken(
"1*******************************X")
.setOAuthAccessTokenSecret(
"1***************************c");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
try {
List<Status> statuses;
String user;
user = "replace with the user name of your choice";
statuses = twitter.getUserTimeline(user);
Log.i("Status Count", statuses.size() + " Feeds");
for (int i = 0; i < statuses.size(); i++) {
Status status = statuses.get(i);
Log.i("Tweet Count " + (i + 1), status.getText() + "\n\n");
}
} catch (TwitterException te) {
te.printStackTrace();
}
代码示例来源:origin: stackoverflow.com
String consumerKey = "yourconsumerKey ";
String consumerSecret = "yourconsumerSecret";
String accessToken = "yourAccessToken";
String accessSecret = "yourAccessSecret";
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(accessSecret);
try
{
TwitterFactory factory = new TwitterFactory(cb.build());
Twitter twitter = factory.getInstance();
System.out.println(twitter.getScreenName());
Status status = twitter.updateStatus(latestStatus);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
}catch (TwitterException te) {
te.printStackTrace();
System.exit(-1);
}
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder
.setOAuthConsumerKey(STAConstants.TWITTER_CONSUMER_KEY);
configurationBuilder
.setOAuthConsumerSecret(STAConstants.TWITTER_CONSUMER_SECRET);
Configuration configuration = configurationBuilder.build();
TwitterFactory twitterFactory = new TwitterFactory(configuration);
Twitter twitter = twitterFactory.getInstance();
AccessToken accessToken = new AccessToken("ACCESS_TOKEN","TOKEN_SECRET");
twitter.setOAuthAccessToken(accessToken);
System.out.println(twitter.getUserTimeline("twitter"));
代码示例来源:origin: rjyo/twitter-2-weibo
Twitter twitter = new TwitterFactory().getInstance();
if (user.getTwitterToken() != null) {
twitter.setOAuthAccessToken(new AccessToken(user.getTwitterToken(), user.getTwitterTokenSecret()));
log.debug(String.format("Using OAuth for %s", user.getUserId()));
List<Status> statuses = twitter.getUserTimeline(screenName);
if (statuses.size() > 0) {
user.setLatestId(statuses.get(0).getId()); // Record latestId, and sync next time
Timeline tl = new Timeline();
Paging paging = new Paging(latestId);
List<Status> statuses = twitter.getUserTimeline(screenName, paging);
Status status = statuses.get(i);
if (status.getId() < user.getLatestId()) continue; // safe keeper
String name = status.getUser().getScreenName();
String statusText = status.getText();
log.info(String.format("@%s - %s", name, statusText));
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");
query.setCount(numberOfTweets - tweets.size());
try {
QueryResult result = twitter.search(query);
tweets.addAll(result.getTweets());
println("Gathered " + tweets.size() + " tweets");
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(Consumer__Key);
configurationBuilder.setOAuthConsumerSecret(Consumer_Secret);
Configuration configuration = configurationBuilder.build();
Twitter twitter = new TwitterFactory(configuration).getInstance("username","password");
AccessToken token = twitter.getOAuthAccessToken();
System.out.println("Access Token " +token );
String name = token.getScreenName();
System.out.println("Screen Name" +name);
PrintWriter out= response.getWriter();
System.out.println(token);
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("your key")
.setOAuthConsumerSecret(
"your key")
.setOAuthAccessToken(
"your key")
.setOAuthAccessTokenSecret(
"your key");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
Query query = new Query("#iphone7");
query.setCount(100);
try {
result = twitter.search(query);
tweets.addAll(result.getTweets());
System.out.println("Gathered " + tweets.size() + " tweets");
} catch (TwitterException te) {
System.out.println("Couldn't connect: " + te.toString());
}
代码示例来源:origin: stackoverflow.com
TwitterFactory twitterFactory = new TwitterFactory();
Twitter twitter = twitterFactory.getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
twitter.setOAuthAccessToken(new AccessToken(accessToken, accessTokenSecret));
User user = twitter.verifyCredentials();
List<Status> statuses = twitter.getMentionsTimeline();
System.out.println("Showing @" + user.getScreenName() + "'s mentions.");
for (Status status : statuses) {
System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
te.printStackTrace();
System.out.println("Failed to get timeline: " + te.getMessage());
代码示例来源:origin: stackoverflow.com
protected String doInBackground(String... args) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(CONSUMER_KEY);
builder.setOAuthConsumerSecret(CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
try {
twitter4j.Status response = twitter.updateStatus(tweetText);
return response.toString();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
代码示例来源:origin: stackoverflow.com
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(consumerKey);
builder.setOAuthConsumerSecret(SecretKet);
User user;
AccessToken newAcc = new AccessToken(getoken, getokensecret);
Twitter twitter = new TwitterFactory(builder.build())
.getInstance(newAcc);
try {
String mUserName = twitter.getAccountSettings().getScreenName();
user = twitter.showUser(mUserName);
String username = user.getName();
String email= user.getEmail();
String oauth_uid = Integer.toString((int) user.getId());
} catch (TwitterException e) {
Log.v("ERROR", "API_Get_Twitter_Registration_DATA"+e.toString())
e.printStackTrace();
}
代码示例来源:origin: stackoverflow.com
Configuration configuration = new ConfigurationBuilder()
.setOAuthConsumerKey("your_customer_key")
.setOAuthConsumerSecret("your_customer_secret")
.build();
Twitter twitter = new TwitterFactory(configuration).getInstance(new BasicAuthorization("username", "password"));
AccessToken token = twitter.getOAuthAccessToken();
内容来源于网络,如有侵权,请联系作者删除!