How to Retweet a tweet using REST API


#1

Hi.
I am new to the Twitter REST API . I managed to pull tweets using the REST API but now i want to retweet a specific tweet. I have searched on the internet and have not found any guide on this. Am doing it in android using Http Request. Any help in Java will be much appreciated.


#2

You’ll want to use this for retweeting a tweet: https://dev.twitter.com/rest/reference/post/statuses/retweet/%3Aid

You will need to build the OAuth headers etc the same way as you did for fetching tweets, but also make sure your application has “Read & Write” permissions (check on apps.twitter.com) for retweeting.


#3

IgorBrigadir thank you for your response but i did the same and the error am getting is FileNotFoundException https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json What mightmight be the problem?


#4

How are you invoking the API call? are you using twurl or similar? Are you able to share any code?


#5

This is the connection piece of code. What am i doing wrong?

HttpURLConnection httpConnection = null;
        BufferedReader bufferedReader = null;
        StringBuilder response = new StringBuilder();
        String msg = "";

        try {
            URL url = new URL("https://api.twitter.com/1.1/statuses/retweet/241259202004267009.json");
            httpConnection = (HttpURLConnection) url.openConnection();
            httpConnection.setDoInput(true);
            httpConnection.setDoOutput(true);
            httpConnection.setRequestMethod("POST");
            String jsonString = auth;
            JSONObject jsonObjectDocument = new JSONObject(jsonString);
            String token = jsonObjectDocument.getString("token_type") + " "
                    + jsonObjectDocument.getString("access_token");
            httpConnection.setRequestProperty("Authorization", token);
            httpConnection.setRequestProperty("Content-Type",
                    "application/json");
            httpConnection.connect();
            StringBuffer json = new StringBuffer();

            OutputStreamWriter writer = new OutputStreamWriter(httpConnection.getOutputStream(), "UTF-8");
            writer.write(token);
            writer.close();
            if (httpConnection.getResponseCode() / 100 == 2) { // 2xx code means success
                BufferedReader br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    json.append(line);
                }
                br.close();
            } else {
                json.append(httpConnection.getErrorStream());
            }

            httpConnection.disconnect();
//                String line;
//                while ((line = bufferedReader.readLine()) != null) {
//                    response.append(line);
//                }
            result = json.toString();


        } catch (Exception e) {
            result = "GET error: \n" + Log.getStackTraceString(e);

        } finally {
            if (httpConnection != null) {
                httpConnection.disconnect();

            }

#6

Your authorization doesn’t look like valid oauth.