2011-03-17 58 views
5

我一直在尋找一種方式來發布我的Android應用程序的推文,但所有我發現的東西都不起作用。使用Android API發佈推文

我不得不承認,Twitter的API並不那麼容易理解,但我的代碼並不長,而且我也沒有看到我的錯誤在哪裏。

這是我的代碼:

public class PartagerTwitter extends Activity { 

    private CommonsHttpOAuthConsumer httpOauthConsumer; 
    private OAuthProvider httpOauthprovider; 
    public final static String consumerKey = XXX; 
    public final static String consumerSecret = XXX; 
    private final String CALLBACKURL = "myapp://twitactivity"; 
    private Twitter twitter; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     try { 
      httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); 
      httpOauthprovider = new DefaultOAuthProvider("https://twitter.com/oauth/request_token", 
        "https://twitter.com/oauth/access_token", 
      "https://twitter.com/oauth/authorize"); 
      String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); 

      this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 

     super.onNewIntent(intent); 
     Uri uri = intent.getData(); 
     Log.i("test", uri.toString()); 
     if (uri != null && uri.toString().startsWith(CALLBACKURL)) { 
      String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); 
      try { 
       httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);  
       twitter = new TwitterFactory().getInstance(); 
       twitter.setOAuthConsumer(consumerKey, consumerSecret); 

       RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL); 
       AccessToken accessToken = twitter.getOAuthAccessToken(requestToken); 
       String token = accessToken.getToken(), 
       secret = accessToken.getTokenSecret(); 

       accessToken = new AccessToken(token,secret); 
       Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken); 
       twitter.updateStatus("My First Status Update"); 
      } catch (Exception e) { 
       e.printStackTrace(); 

      } 
     } 
    } 
} 

當然,我的鑰匙不是XXX。 另一個問題是,登錄後有可能關閉Twitter窗口並將我的活動置於前臺嗎?

回答

0

在Twitter開發站點創建具有讀/寫權限的應用程序,爲您的應用程序生成訪問令牌。

使用下面的代碼更新狀態

public static void main(String[] args) { 

    String CONSUMER_KEY = "YOUR_CONSUMER_KEY"; 
    String CONSUMER_SECRET = "YOUR_CONSUMER_SECRET"; 
    String ACCESS_SECRET = "YOUR_ACCESS_SECRET"; 
    String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; 

    // Consumer 
    Twitter twitter = new TwitterFactory().getInstance(); 
    twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 

    // Access Token 
    AccessToken accessToken = null; 
    accessToken = new AccessToken(ACCESS_TOKEN, ACCESS_SECRET); 
    twitter.setOAuthAccessToken(accessToken); 

    // Posting Status 
    Status status = null; 
    try { 
     status = twitter.updateStatus("YOUR_STATUS"); 
    } catch (TwitterException e) { 
     e.printStackTrace(); 
    } 
    System.out.println("Successfully updated the status: " 
      + status.getText()); 
}