由于更新限制错误,无法将图片发布到Twitter

时间:2014-11-29 17:24:35

标签: android twitter

我正在尝试使用以下代码将图像上传到Twitter,但我收到了一个限制错误:

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);

第一张图片是139 KB,第二张图片是140 KB大。这两个图像都在网络上,String_to_File方法将它们下载到SD卡。

我确信我没有达到我知道的任何限制,因为我无法发布图像,但我可以发送一条消息。 但是,我认为这个String_to_File方法存在一些问题,因为它将第一个(存根)图像保存到SD卡,但是当它保存第二个时,它已经坏了或因为我无法打开它。

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;
}

错误:

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)

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

我发现了问题并且与Twitter无关,因此应删除此主题。

问题是上传到我服务器的图像名称中的特殊字符已被删除,因此存在不匹配。这就是为什么String_to_File()无法下载图像,因为它正在寻找my_image.png,但它找到的只是myimage.png。

答案 1 :(得分:0)

如果您认为库中没有错误,那么您可能没有为请求使用安全连接。请参阅此处的Twitter文档:

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