How to post a tweet from an Android app to one specific account?

青春壹個敷衍的年華 提交于 2019-12-03 08:26:06

I have found a solution for this problem. Thought I share it here in case anyone has the same problem.

  1. First you need to create the account for your app on twitter
  2. Go to this page and log in with the created account
  3. Move your mouse over your account name in the upper right corner and click "My applications"
  4. Click on "Create a new application" on the right
  5. Fill in the form and create the application
  6. Go to Settings and under Application type set the Access type to "Read and Write" and save the settings
  7. Return to the "Details" page and scroll down to the bottom and click the "Create access token" button
  8. Use these generated tokens and the other ones ("Consumer key" and "Consumer secret") which are shown above these previously generated tokens on the same page to post tweets from your app

Here's the code I used in my app:

You need to include the twitter4j-core-android-2.2.5.jar package for this. You can download it from here: http://twitter4j.org/archive/twitter4j-android-2.2.5.zip

    tweet=(Button)findViewById(R.id.tweetbtn);
    message=(EditText)findViewById(R.id.messagetxt);

    tweet.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            String token ="<Your access token>"; 
            String secret = "<Your access token secret>";
            AccessToken a = new AccessToken(token,secret);
            Twitter twitter = new TwitterFactory().getInstance();
            twitter.setOAuthConsumer("<Your consumer key>", "<Your consumer secret>");
            twitter.setOAuthAccessToken(a);
            try {
                twitter.updateStatus(message.getText().toString());
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

the new link for download

http://twitter4j.org/archive/twitter4j-android-2.2.5.zip

old link does not work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!