2012-03-26 79 views
1
public class TwitterActivity extends Activity 
    { 
     private Twitter twitter; 
     private OAuthProvider provider; 
     private CommonsHttpOAuthConsumer consumer; 

     String CONSUMER_KEY = "abcdefgh"; 
     String CONSUMER_SECRET = "abcdefgh"; 
     String CALLBACK = "twitterapp://connect"; 


     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      askOAuth(); 
     } 

     private void askOAuth() { 
      try { 
       consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
       provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", 
                "http://twitter.com/oauth/access_token", 
                "http://twitter.com/oauth/authorize"); 
       String authUrl = provider.retrieveRequestToken(consumer, CALLBACK); 
       Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show(); 
       this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); 
      } catch (Exception e) { 
       Log.e(APP, e.getMessage()); 
       Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     protected void onNewIntent(Intent intent) { 

      super.onNewIntent(intent); 

      Uri uri = intent.getData(); 
      if (uri != null && uri.toString().startsWith(CALLBACK)) { 

       String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); 

       try { 
        // this will populate token and token_secret in consumer 
        provider.retrieveAccessToken(consumer, verifier); 

        // TODO: you might want to store token and token_secret in you app settings!!!!!!!! 
        AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret()); 

        // initialize Twitter4J 
        twitter = new TwitterFactory().getInstance(); 
        twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
        twitter.setOAuthAccessToken(a); 

        // create a tweet 
        Date d = new Date(System.currentTimeMillis()); 
        String tweet = "#OAuth working! " + d.toLocaleString(); 

        // send the tweet 
        twitter.updateStatus(tweet); 

       } catch (Exception e) { 
        Log.e(APP, e.getMessage()); 
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 

      } 
     } 
    } 


P.S.: I have used these api's 
    [1]: https://github.com/punitmg/Twitter-Test-App 
    [2]: https://github.com/grantland/twitter-android-sdk 
    [3]: https://github.com/yusuke/twitter4j/ 

通過使用所有這些API的我能夠鳴叫成功。但不幸的是,下面提到的2個屏幕出現在所有上述3種情況下。Twitter回撥和登錄使用Jtwitter

我想要的是當我的推文完成時完成()或關閉下面的兩個屏幕。

So .... plz guide me if i was wrong ... 

screenshot1

screenshot2

+0

我不想要上面的兩個屏幕,他們仍然在堆棧,每當我推特 – 2012-03-26 14:40:17

回答

0

您需要強制登錄第二次來解決這個問題,

因此,只要在你的Twitter授權網址添加force_login =真

我的意思只是改變了下面的代碼行中TwitterApp.java類(如果你有不同名稱的類文件,然後在您的項目與搜索DefaultOAuthProvider

mHttpOauthprovider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", 
       "http://twitter.com/oauth/access_token", 
     "http://twitter.com/oauth/authorize?force_login=true"); 

加入後force_login = true webview加載問題將被解決,但每次您需要輸入登錄名和密碼。