Show me the code! – By Davanum Srinivas

March 16, 2009

Post/Update Twitter status snippet (from java)

Filed under: Uncategorized — Davanum Srinivas @ 10:10 am

Snippet to post to twitter. Using sun.misc to avoid introducing another jar (say commons codec). Please feel free to use another Base64 implementation. Rest of the details will remain the same.

import sun.misc.BASE64Encoder;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.net.URLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Twitter {
    public static void main(String[] args) throws Exception {
        if (args.length < 3) {
            System.out.println("Twitter <userid> <password> <message>");
            System.exit(-1);
        }

        URL url = new URL("https://twitter.com/statuses/update.xml");
        URLConnection connection = url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        String authorization = args[0] + ":" + args[1];
        BASE64Encoder encoder = new BASE64Encoder();
        String encoded = new String
                (encoder.encodeBuffer(authorization.getBytes())).trim();
        connection.setRequestProperty("Authorization", "Basic " + encoded);

        OutputStreamWriter out = new OutputStreamWriter(
                connection.getOutputStream());
        out.write("status=" + URLEncoder.encode(args[2], "UTF-8"));
        out.close();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        connection.getInputStream()));
        String response;
        while ((response = in.readLine()) != null) {
            System.out.println(response);
        }
        in.close();

    }
}

5 Comments »

  1. […] in a “Build Successful” message. If Java is your thing, look at Davanum Srinivas’ post and for .NET, there is […]

    Pingback by Build Management 2.0 - Messaging — April 3, 2009 @ 7:43 pm

  2. I am getting the error : java.io.IOException: Server returned HTTP response code: 401 for URL: http://twitter.com/statuses/update.xml

    when i am trying to run this code as a standalone application

    Comment by kiran — November 30, 2010 @ 4:36 am

  3. I tried to execute this program and i got the following exception:
    Exception in thread “main” java.io.IOException: Server returned HTTP response code: 401 for URL: https://twitter.com/statuses/update.xml
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
    What could be the issue?

    Comment by Teja — December 9, 2010 @ 3:14 pm

  4. i also run above code and i getting the below exception..now what can i do ?please help me..

    error:

    Exception in thread “main” java.io.IOException: Server returned HTTP response code: 401 for URL: https://twitter.com/statuses/update.xml
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at twxa.Twitter.main(Twitter.java:62)

    Comment by lingadurai — July 15, 2011 @ 12:37 am

  5. Error 401 means that you do not have access, did you pass the right arguments to the program (login and password), see line 24?
    See the different HTTP response codes on Twitter: https://dev.twitter.com/docs/streaming-api/response-codes

    If that does not fix the problem, take a look at line 26, and make sure (while debugging) that there is not “\n\r” appended to the encoded variable.

    Comment by Ted Gueniche — December 18, 2011 @ 9:16 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: