Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post a tweet with Android API

I've been searching for a way to post a tweet with my Android application, but all the things I've found don't work.

I have to admit that Twitter's API isn't so simple to understand, but my code isn't really long, and I don't see where is my mistake.

This is my code:

public class PartagerTwitter extends Activity {

    private CommonsHttpOAuthConsumer httpOauthConsumer;
    private OAuthProvider httpOauthprovider;
    public final static String consumerKey = XXX;
    public final static String consumerSecret = XXX;
    private final String CALLBACKURL = "myapp://twitactivity";
    private Twitter twitter;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
            httpOauthprovider = new DefaultOAuthProvider("https://twitter.com/oauth/request_token",
                    "https://twitter.com/oauth/access_token",
            "https://twitter.com/oauth/authorize");
            String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL);

            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);  
        Uri uri = intent.getData();
        Log.i("test", uri.toString());  
        if (uri != null && uri.toString().startsWith(CALLBACKURL)) {
            String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
            try {
                httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier);     
                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(consumerKey, consumerSecret);

                RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACKURL); 
                AccessToken accessToken = twitter.getOAuthAccessToken(requestToken);
                String token = accessToken.getToken(), 
                secret = accessToken.getTokenSecret();

                accessToken = new AccessToken(token,secret);
                Twitter twitter = new TwitterFactory().getOAuthAuthorizedInstance(consumerKey,consumerSecret,accessToken);
                twitter.updateStatus("My First Status Update");
            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }
}

Of course, my keys aren't XXX. And another question, is it possible, after login, to close the Twitter window and bring my activity to the foreground?

like image 351
user664653 Avatar asked Mar 17 '11 16:03

user664653


People also ask

Can I tweet using Twitter API?

The Twitter API is simply a set of URLs that take parameters. They URLs let you access many features of Twitter, such as posting a tweet or finding tweets that contain a word, etc. Twitter allows you to interact with its data tweetsand several attributes about tweets using twitter API.

How do you post a tweet on Android?

To post a Tweet:Select the Tweet icon. Enter your message, and then select Tweet. A notification will appear in the status bar on your device and will go away once the Tweet successfully sends.

Can I use Twitter API for free?

The Twitter API v2 includes a few access levels to help you scale your usage on the platform. In general, new accounts can quickly sign up for free, Essential access. Should you want additional access, you may choose to apply for free Elevated access and beyond.


1 Answers

Please check the following blog post for a complete end-to-end guide for posting to Twitter from an Android app.

It also includes a working Android sample in Github.

like image 154
ddewaele Avatar answered Oct 11 '22 17:10

ddewaele