开发者

integrating twitter in my android application

开发者 https://www.devze.com 2023-02-09 22:10 出处:网络
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. h

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号