如何在Android上发送Twitter推文?

时间:2012-05-11 03:28:44

标签: android service oauth signpost jtwitter

有没有人在Android上通过jTwitter成功发送推文?

我坚持使用Authentification。我可以成功将用户重定向到Twitter,授权我的应用并使用new OAuthSignpostClient(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL)重定向到我的应用。然后,我存储了使用回调网址提供的oauth_tokenoauth_verifier,并尝试再次使用OAuthSignpostClient来更新状态:

OAuthSignpostClient client = new OAuthSignpostClient(TwitterOAuthActivity.CONSUMER_KEY, TwitterOAuthActivity.CONSUMER_SECRET, accessToken, accessTokenSecret);

// Ready to go!
Twitter twitter = new Twitter(null, client);
CharSequence date = DateFormat.format("dd.MM.yyyy @ hh:mm:ss", new Date());
twitter.updateStatus("Yay. It works! " + date);

哪一个以TwitterException结尾而没有由:

引起
05-11 12:24:32.643: E/AndroidRuntime(25897): winterwell.jtwitter.TwitterException$E401: Could not authenticate with OAuth.
05-11 12:24:32.643: E/AndroidRuntime(25897): http://api.twitter.com/1/statuses/update.json (anonymous)
05-11 12:24:32.643: E/AndroidRuntime(25897):    at winterwell.jtwitter.URLConnectionHttpClient.processError(URLConnectionHttpClient.java:425) 
05-11 12:24:32.643: E/AndroidRuntime(25897):    at winterwell.jtwitter.OAuthSignpostClient.post2_connect(OAuthSignpostClient.java:345)

有没有人知道我的问题在哪里?

1 个答案:

答案 0 :(得分:0)

来自回调网址的验证者是临时密钥。它让你解锁你拥有的OAuthSignpostClient对象。您无法使用它来构建新的OAuthSignpostClient。

您需要致电:

client.setAuthorizationCode(verifier);
// The client can now be used!
// To use it again, without the oauth dance, store _these_ tokens:
String[] tokens = client.getAccessToken();

您可能还想查看新的AndroidTwitterLogin类,这样可以轻松实现:

AndroidTwitterLogin atl = new AndroidTwitterLogin(myApp, 
                MY_TWITTER_KEY,MY_TWITTER_SECRET,MY_TWITTER_CALLBACK) {                 

    protected void onSuccess(Twitter jtwitter, String[] tokens) {
        jtwitter.setStatus("I can now post to Twitter!");
        // Recommended: store tokens in your app for future use
        // with the constructor OAuthSignpostClient(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret)
    }
};
atl.run();