integrating twitter in my android application
i am integrating the twitter in to my android application. and it logins me successfully on twitter but i do not know how to get the tweets and statuses from that so i can show it in my application. here is my code.
String CONSUMER_KEY = "XXXXXXXXXXXXXXX";
String CONSUMER_SECRET = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KE开发者_Go百科Y, CONSUMER_SECRET);
requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
String url = requestToken.getAuthorizationURL();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
catch (TwitterException e){
e.printStackTrace();
}
any help will be appreciated.
You can handle the login part, without the user introducing the pin if you use signpost, and once you have the token and the verifier, you can go on with twitter4j, creating the object twitter with TwitterFactory twitterfact=new TwitterFactory();
twitter = twitterfact.getOAuthAuthorizedInstance(consumerKey, consumerSecret,accessToken);
Now you can show the timeline with twitter.getFriendsTimeLine()
. It's what I do, and it works fine. I can tweet, read tweets, send private messages... and the login part doesn't fail.
As soon as you have the twitter
variable correctly populated (i.e. your code throws no Exception), you can use it to twitter.getHomeTImeline()
etc.
Having said that, your code looks like it only does the first part of the OAuth procedure and that you still need to have code that sets the pin the user receives and then creates a fully OAuth authorized connection.
Have e.g. a look at LoginActivity of Zwitscher (v0.65 tag).
The method getPinButton()
is basically what you have above. When the user comes back, he enters the pin in an EditText and clicks on the [setPinButton()][2]
which provides the 2nd part of the OAuth stuff.
OAuth keyes and tokens are then stored in preferences for later use (you need them to create authenticated Twitter instances via the TwitterFactory (see e.g. TwitterHelper.getTwitter()
on how to do this).
精彩评论