使用twitter4j的Android Twitter xAuth示例

时间:2010-08-20 05:47:12

标签: android twitter twitter4j xauth

Android:我正在为android分享尝试以下xAuth示例。

xAuth Authentication for Twitter Share in Android?

System.setProperty("twitter4j.oauth.consumerKey", "your token");
System.setProperty("twitter4j.oauth.consumerSecret", "your token secret");

Twitter twitter = new TwitterFactory().getInstance(login, password);

AccessToken accessToken = twitter.getOAuthAccessToken();
//Then you must save your Token and Token secret from AccesToken

if (mAccessToken != null) {
    if (mAccessToken.getToken() != null && mAccessToken.getTokenSecret() != null) {
        saveAccessToken(mAccessToken.getToken(), mAccessToken.getTokenSecret());
    }
}

我有IllegalStateException

Exception Msg: java.lang.IllegalStateException: OAuth consumer key/secret combination not supplied

在以下一行

AccessToken accessToken = twitter.getOAuthAccessToken();

我做了一些更改,如下面的

Twitter twitter = new TwitterFactory().getInstance("login", "pass");
            twitter.setOAuthConsumer(getString(R.string.twtAPIKey), getString(R.string.twtSecret));
            AccessToken mAccessToken = twitter.getOAuthAccessToken();

我又得到了例外 Exception Msg: java.lang.IllegalStateException: Basic authenticated instance.

xAuth的任何工作示例?

2 个答案:

答案 0 :(得分:5)

我使用了以下


ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

     configurationBuilder.setOAuthConsumerKey(Consumer__Key);
     configurationBuilder.setOAuthConsumerSecret(Consumer_Secret);
     Configuration configuration = configurationBuilder.build();

     Twitter twitter = new TwitterFactory(configuration).getInstance("username","password"); 

     AccessToken token = twitter.getOAuthAccessToken();
     System.out.println("Access Token " +token );

     String name = token.getScreenName();
     System.out.println("Screen Name" +name);

     PrintWriter out= response.getWriter();
     System.out.println(token);

使用Android应用程序使用xauth

成功登录Twitter

答案 1 :(得分:2)

twitter4j的2.2.2版略有变化。这有效:

Configuration configuration = new ConfigurationBuilder()
.setOAuthConsumerKey("your_customer_key")
.setOAuthConsumerSecret("your_customer_secret")
.build();

Twitter twitter = new TwitterFactory(configuration).getInstance(new BasicAuthorization(username, password)); // yes, use "BasicAuthorization" although that seems strange

AccessToken token = twitter.getOAuthAccessToken();
Log.d(TAG, "Access token: " + token.getToken());
Log.d(TAG, "Access token secret: " + token.getTokenSecret());

请记住,在此之前,您的Twitter客户端应用程序需要authorized to use xAuth