Search code examples
javaandroidapitwittertwitter4j

Uploading Video on twitter using HTTP API


Hi I am trying to upload Video using REST APIs of twitter Twitter POST video Request

In this documentation, I am not able to understand how can I make this command from my Android Code.:

twurl -H upload.twitter.com "/1.1/media/upload.json" -d "command=INIT&media_type=video/mp4&total_bytes=4430752"

Also, I am able to post status updates and images using twitter4j, do I need to reAuthenticate to use HTTP APIs:

protected Void doInBackground(String... args) {
            Date date = new Date();
            String status = date.toString();
            Log.d("Status", status);
            try {
                ConfigurationBuilder builder = new ConfigurationBuilder();
                builder.setOAuthConsumerKey(consumerKey);
                builder.setOAuthConsumerSecret(consumerSecret);

                // Access Token
                String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
                // Access Token Secret
                String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");

                AccessToken accessToken = new AccessToken(access_token, access_token_secret);
                Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);

                // Update status
                StatusUpdate statusUpdate = new StatusUpdate("@UserName : " +status);
                if(((MyApplication)getActivity().getApplication()).getLinkPath() != null) { //getting image resource file
                    String s = "file://" + ((MyApplication) getActivity().getApplication()).getLinkPath();
                    Uri uri = Uri.parse(s);
                    InputStream is = getActivity().getContentResolver().openInputStream(uri);
                    Log.d("Logger", "Sending media");
                    statusUpdate.setMedia(s, is);
                    Log.d(s, "Sent media");

                }
                twitter4j.Status response = twitter.updateStatus(statusUpdate);

                Log.d("Status", response.getText());

            } catch (TwitterException e) {
                Log.d("Failed to post!", e.getMessage());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

Solution

  • Update 03/16/2018, New PR and Maven Repo

    I created a pull request to add this feature in twitter4j. You can look at the implementation here: Adding Chuncked, video upload