twitter4j从webintent获取Oauth

时间:2012-12-03 05:23:26

标签: android android-intent twitter-oauth twitter4j

当我尝试从请求中捕获访问令牌时,应用程序崩溃了。我已经有一段时间遇到这种麻烦了。我可以启动web意图,然后在确认后回调应用程序,但后来我不确定如何正确获取返回的令牌。如何在有人验证后获取访问令牌

        12-03 05:19:17.923: E/AndroidRuntime(3494): FATAL EXCEPTION: main
        12-03 05:19:17.923: E/AndroidRuntime(3494): java.lang.RuntimeException: Unable to resume activity {com.example.twwetking/com.example.twwetking.Tweetr}: java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/configuration.html for the detail.


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tweetr);

    //create a new twitter configuration using user details
    Configuration twitConf = new ConfigurationBuilder()
        .setOAuthConsumerKey(TWIT_KEY)
        .setOAuthConsumerSecret(TWIT_SECRET)

        .build();

        //create a twitter instance
    tweetTwitter = new TwitterFactory(twitConf).getInstance();
    Button tweetr = (Button)findViewById(R.id.tweetr);


    tweetr.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            dt.execute(points);

        }
    });



}


public class TweetTask extends AsyncTask<Object, Void, String> {
    @Override
    protected String doInBackground(Object... values) {

        try {
            requestToken = tweetTwitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));

        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      return null;
    }

    @Override
    protected void onPostExecute(String result) {


    }
}



@Override
protected void onResume() {
    super.onResume();
    if (this.getIntent()!=null && this.getIntent().getData()!=null) {  
        Log.d("KEY", requestToken.getToken().toString()+" SECRET   "+requestToken.getTokenSecret().toString());
        ACCESS_TOKEN = requestToken.getToken().toString();
        ACCESS_TOKEN_SECRET = requestToken.getTokenSecret().toString();
        try {
            tweetTwitter.updateStatus("I just made a new Top Score On JamTrivia "+points+"points!!!");
        } catch (TwitterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

使用此链接在Android应用程序中集成twitter,这将适用于所有Android版本。

https://github.com/lorensiuswlt/AndroidTwitter