4

I have to add an option to my game for posting highscores to twitter. The idea is that the app has it's own twitter account and the user can upload the score to this specific account with a click on a button or a menu item (haven't decided how the UI looks like yet).

I have found many tutorials like this: http://blog.doityourselfandroid.com/2011/02/13/guide-to-integrating-twitter-android-application/ ,which show how to post to twitter from apps but in all of these solutions the user needs to login with his/her own account.

Any suggestions are welcome. Thank you.

NNorbi90
  • 151
  • 2
  • 11

2 Answers2

11

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();
            }

        }
    });
NNorbi90
  • 151
  • 2
  • 11
  • good job @NNorbi! this is also work for me. But i want to send tweet from my account to any other account. Like u can send your score in to a tweet from your twitter user account to @androidjeetu twitter user account. agree then comment thanks! – Jeetu Jan 30 '13 at 08:58
  • Hello @Jeetu. I'm not sure I understood your question exactly, but if you just want to post some message to the users account, you can follow the tutorial mentioned in my question or you can just start a new intent for sending a plain-text message like in this tutorial: [link](http://sudarmuthu.com/blog/sharing-content-in-android-using-action_send-intent) However, for the second method the user will need the twitter app to be installed. PS: Sorry for the late answer. I have been a bit busy these days. – NNorbi90 Feb 23 '13 at 21:22
  • OH Thanks for reply @nnorbi! but i got the answer of my question. u have need the screen name of the user and u can send tweet. – Jeetu Feb 25 '13 at 06:03
  • @Jeetu Can you post your code or other points to keep in mind so that others in need, like me, can look at it? Thank you. – Geek Jul 05 '13 at 06:20
  • @Akash what is your exact problem? Did u post your question here if yes then give me link of your question. – Jeetu Jul 06 '13 at 07:38
  • @Jeetu This is my question I asked today http://stackoverflow.com/questions/17499935/android-twitter-integration-using-oauth-2-and-twitter4j-core-3-0-3. Basically I tried codes from few tutorials but none of them work. If you can show your working code then I can try it or find a problem in my case. – Geek Jul 06 '13 at 07:57
  • point 7 must be updated: now you can create access token from "API Keys" label. – JJ86 Aug 20 '14 at 13:30
  • @Jeetu: I have a same problem as you. I would really appreciate it if you could post the code for your solution. – G droid Dec 28 '15 at 18:14
  • @NNorbi90 hello! Do you have any idea of how to do the above using cordova (http, css, js)? thanks! – Sarah cartenz Feb 26 '16 at 19:54
1

the new link for download

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

old link does not work