1

I am trying to upload an image to Twitter with the following code but I get a limit error:

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TwitterConst.CONSUMER_KEY);
builder.setOAuthConsumerSecret(TwitterConst.CONSUMER_SECRET);

String access_token = mSharedPreferences.getString(TwitterConst.PREF_KEY_TOKEN, "");
String access_token_secret = mSharedPreferences.getString(TwitterConst.PREF_KEY_SECRET, "");

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

StatusUpdate status = new StatusUpdate("Sample message: " + args[0] + " - Download it from Google Play at goo.gl/xxxxxx");
if (foto_to_share.equals("https://www.mywebsite.com/Folder/default/fb_stub.png")) {
    status.setMedia(String_to_File("https://www.mywebsite.com/Folder/default/fb_stub.png"));
} else {
    status.setMedia(String_to_File("http://www.mywebsite.com/Folder/upload/" + session_userid + "/" + foto_to_share));
 }
 twitter4j.Status response = twitter.updateStatus(status);

The first image is 139 KB, the second one is 140 KB large. Both images are on the web, the String_to_File method downloads them to the SD card.

I am sure I haven't reached any limits I know as I can't post the image, but I can tweet a message. However, I think there is some problem with this String_to_File method as it saves the first (stub) image to the SD card, but when it saves the second one, it's broken or something because I can't open it.

public File String_to_File(String img_url) {

            try {
                File rootSdDirectory = Environment.getExternalStorageDirectory();

               casted_image = new File(rootSdDirectory, "attachment.jpg");
                if (casted_image.exists()) {
                casted_image.delete();
                }
                casted_image.createNewFile();

                FileOutputStream fos = new FileOutputStream(casted_image);

                URL url = new URL(img_url);
                HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.connect();
                InputStream in = connection.getInputStream();

                byte[] buffer = new byte[1024];
                int size = 0;
                while ((size = in.read(buffer)) > 0) {
                fos.write(buffer, 0, size);
                }
                fos.close();
                return casted_image;

            } catch (Exception e) {

                System.out.print(e);
                // e.printStackTrace();

            }
            return casted_image;
}

The error:

11-29 18:14:23.090: W/System.err(14264): 403:The request is understood, but it has been refused. An accompanying error message will explain why. This code is used when requests are being denied due to update limits (https://support.twitter.com/articles/15364-about-twitter-limits-update-api-dm-and-following).
11-29 18:14:23.090: W/System.err(14264): message - Error creating status.
11-29 18:14:23.090: W/System.err(14264): code - 189
11-29 18:14:23.090: W/System.err(14264): Relevant discussions can be found on the Internet at:
11-29 18:14:23.090: W/System.err(14264):    http://www.google.co.jp/search?q=2fc5b7cb or
11-29 18:14:23.090: W/System.err(14264):    http://www.google.co.jp/search?q=0e8e77fd
11-29 18:14:23.090: W/System.err(14264): TwitterException{exceptionCode=[2fc5b7cb-0e8e77fd], statusCode=403, message=Error creating status., code=189, retryAfter=-1, rateLimitStatus=null, version=4.0.2}
11-29 18:14:23.090: W/System.err(14264):    at twitter4j.HttpClientImpl.handleRequest(HttpClientImpl.java:163)
11-29 18:14:23.090: W/System.err(14264):    at twitter4j.HttpClientBase.request(HttpClientBase.java:53)
11-29 18:14:23.090: W/System.err(14264):    at twitter4j.HttpClientBase.post(HttpClientBase.java:82)
11-29 18:14:23.090: W/System.err(14264):    at twitter4j.TwitterImpl.post(TwitterImpl.java:1592)
11-29 18:14:23.090: W/System.err(14264):    at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:213)
11-29 18:14:23.090: W/System.err(14264):    at com.example.blgui3.BucketProfileActivity$updateTwitterStatusImage.doInBackground(BucketProfileActivity.java:3619)
11-29 18:14:23.090: W/System.err(14264):    at com.example.blgui3.BucketProfileActivity$updateTwitterStatusImage.doInBackground(BucketProfileActivity.java:1)
11-29 18:14:23.090: W/System.err(14264):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-29 18:14:23.090: W/System.err(14264):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-29 18:14:23.090: W/System.err(14264):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-29 18:14:23.090: W/System.err(14264):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-29 18:14:23.100: W/System.err(14264):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-29 18:14:23.100: W/System.err(14264):    at java.lang.Thread.run(Thread.java:864)

Any ideas?

erdomester
  • 11,789
  • 32
  • 132
  • 234

2 Answers2

1

I found the problem and it was not Twitter related, so this topic should be deleted.

The problem was that the special characters in the names of the images uploaded to my server were removed so there was a mismatch. This is why the String_to_File() couldn't download the image because it was looking for my_image.png, but all it found was myimage.png.

erdomester
  • 11,789
  • 32
  • 132
  • 234
0

If you don't think it's an error in the library, then you are probably not using a secure connection for the request. See the Twitter docs here:

https://dev.twitter.com/overview/api/ssl

Jim
  • 10,172
  • 1
  • 27
  • 36