Twitter4J - Looking up profile details without logging in
I've been using Twitter4J开发者_开发百科 for a quite a while now, but I can't seem to find this particular feature.
I want to be able to search on a name, and when the certain user is on twitter, I want to retrieve basic information such as tweets, followers (like you can access via http) - but how to do is in Twitter4J? Neither the code examples or the source could help me.
Thanks,
William van Doorn
Look at the following: Twitter4J.api.UserMethods That has the methods you seek. The Twitter class directly implements it.
I think u should use twitter4j library, get Access Token then create object of twitter and and then call showuser() it will return user object then with the help of user object u will get all information of login user i will give a sample code to you.
AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
int i=twitter.getId();
User u=twitter.showUser(i);
System.out.println("user name"+u.getName());
My POM.XML
...
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
</dependencies>
...
My Class Test
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;
public final class GetAccountSettings {
private final String twitter_consumer_key = "oKu1emQJUGArI6kFNXNTCZ111";
private final String twitter_consumer_secret = "g3EzljGP91AuqjBtIRjGFbKVEAg5cpxH96D6rWIgk4MbhK5111";
private final static String oauth_token = "2847960911-LYoKllWcTo3VxZMdcLktWDPbLaeOywwrkf2W111";
private final static String oauth_token_secret = "JoCajDpKvvzzOfsfQpar8aqIDmspyrGehvdj3YsCAp111";
public static void main(String[] args) {
try {
GetAccountSettings getAccountSettings = new GetAccountSettings();
// Twitter Conf.
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true).setOAuthConsumerKey(getAccountSettings.twitter_consumer_key).setOAuthConsumerSecret(getAccountSettings.twitter_consumer_secret)
.setOAuthAccessToken(getAccountSettings.oauth_token).setOAuthAccessTokenSecret(getAccountSettings.oauth_token_secret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
User user = twitter.showUser("MR_Camaleon");
System.out.println("\n *************************** INFORMATION TWITTER PROFILE " + user.getScreenName() + " ************************* \n\n");
System.out.println(" Id: " + user.getId() + " \n");
System.out.println(" Screen name: " + user.getScreenName() + " \n");
System.out.println(" Name: " + user.getName() + " \n");
System.out.println(" Description: " + user.getDescription() + " \n");
System.out.println(" Image profile: " + user.getProfileImageURL() + " \n");
System.out.println(" Follorwers: " + user.getFollowersCount() + " \n");
System.out.println(" Friends: " + user.getFriendsCount() + " \n");
System.out.println(" Created date: " + user.getCreatedAt() + " \n");
System.out.println(" Language: " + user.getLang() + " \n");
System.out.println(" Time zone: " + user.getTimeZone() + " \n\n");
System.out.println(" ******************************************************************************************************");
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get account settings: " + te.getMessage());
System.exit(-1);
}
}
}
精彩评论