GoJb
GoJb

Reputation: 1

Upload image twitter4j

I introduced myself to Twitter4j yesterday, and are now testing out features for an upcoming program of mine. As the title suggests, I am trying to upload an image to twitter, without any luck. Here's my code:

import static java.awt.Toolkit.getDefaultToolkit;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;

import java.awt.Image;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.examples.tweets.UploadMultipleImages;
import twitter4j.media.ImageUpload;
import twitter4j.media.ImageUploadFactory;

public final class UpdateStatus {

static File file = new File("/images/Done.jpg");

public static void main(String[] args) {

    for(int i=0;i<2;i++){
        Twitter twitter = new TwitterFactory().getInstance();

        Status status=null;
        try {

            ImageUpload.upload(file,"22");

        } catch (TwitterException e) {
            System.err.println("Shit...");
            System.exit(3);
        }

    }

    System.out.println("Done");

}
}

The image I'm trying to upload is Done.jpg, and is in a folder in the package. I've used this method for images in other programs, so I am pretty sure it works. Though, this gives me an error message before I run the code, saying "Cannot make a static reference to the non-static method upload(File, String) from the type ImageUpload". Any ideas that could help me? :D

Upvotes: 0

Views: 4787

Answers (2)

mbaxi
mbaxi

Reputation: 1301

You need to ensure following before testing your code -

  1. Register your app at https://apps.twitter.com/ and get Oauth tokens to be able to connect your app to Twitter and perform desired action. You will get a consumerKey,consumerAccessToken, accessKey and accessToken.
  2. If you want to post updates, please ensure you configure your app permissions to have a Read and Write access, deafult access is Read Only.

After you have the required access tokens, you need to instantiate a Twitter instance using those tokens. This instance can then be used to perform requisite action. See sample code below to upload an image -

ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder();
twitterConfigBuilder.setDebugEnabled(true);
twitterConfigBuilder.setOAuthConsumerKey("consumerkey");
twitterConfigBuilder.setOAuthConsumerSecret("consumersecret");
twitterConfigBuilder.setOAuthAccessToken("accesstoken");
twitterConfigBuilder.setOAuthAccessTokenSecret("accesstokensecret");

Twitter twitter = new TwitterFactory(twitterConfigBuilder.build()).getInstance();
String statusMessage = "Watch out this interesting offer I came across today";
File file = new File("/images/Done.jpg"); 

StatusUpdate status = new StatusUpdate(statusMessage);
status.setMedia(file); // set the image to be uploaded here.
twitter.updateStatus(status);

Hope this helps.

Upvotes: 6

ImageUpload.upload is not a static method, but an instance method.

You need to create an instance of ImageUpload, and call the method from the instance.

Checking the documentation of ImageUpload, it is an interface. So you'll need to instantiate a class that implements ImageUpload.

Upvotes: 0

Related Questions