如何使用android中的status/update_with_media将图像发布到Twitter

Man*_*dan 2 twitter android

我需要将图片发布到Twitter.我在我的应用程序中集成了Twitter.我需要发送图像,而不是URL链接.我不想使用TwitPic.

我使用以下代码来创建multipart实体.它给出404错误.

Bitmap bm = null;
                String encodedImage = "";
                try {

                    URL aURL = new URL("http://50.57.227.117/blacksheep/uploaded/Detailed_images/961314275649aladdins.jpg");
                    URLConnection conn = aURL.openConnection();
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is, 8192);
                    bm = BitmapFactory.decodeStream(bis);
                    bis.close();
                    is.close();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
                     imageBytes = baos.toByteArray();
                     encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
                     Log.v("encodedImage >>",encodedImage); 

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(
                        "https://api.twitter.com/1.1/statuses/update_with_media.json");
               ByteArrayBody bab = new ByteArrayBody(imageBytes, "forest.jpg");

                MultipartEntity reqEntity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);
              reqEntity.addPart("media", bab);
                reqEntity.addPart("status", new StringBody("test image"));
                postRequest.setEntity(reqEntity);
                HttpResponse response = httpClient.execute(postRequest);
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();

                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                }
                System.out.println("Response: " + s);
            // Update status
            //twitter4j.Status response = twitter.updateStatus(encodedImage);
        //  twitter4j.Status response1 = twitter.updateStatus(status);

            //Log.d("Status", "> " + response1.getText());
        } catch (TwitterException e) {
            // Error in updating status
            Log.d("Twitter Update Error", e.getMessage());
        }
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 5

第一次尝试:

因此,您似乎使用Apache的旧http客户端在您使用的库外部请求,以帮助与twitter,twitter4j集成.我假设您使用的是3.03之前的最新版本,并且不希望您升级.你看,update_with_media是新的,所以我认为你的版本没有实现它.

你正在做的问题是twitter使用oauth进行身份验证.因此,您需要使用您获得的访问令牌"签署"请求.Twitter4j,AFAIK,为您做到这一点.在不破坏身份验证的情况下,您无法使用单独的客户端进行一些调用而无需引用您的好帮助程序库.

端点../update_with_media定义为更新当前身份验证用户的状态.我怀疑,由于您的请求中没有访问令牌和用户,因此该端点甚至没有意义,因此Twitter将其解释为404(未找到)而非401(未经授权) - 有趣.

所以第一次尝试不要求你升级到twitter4j.有时升级很痛苦!相反,您可以通过此博客详细介绍库.但这并不容易,因为图书馆不同.

所以,我们可以尝试的另一件事,如果你真的想对twitter4j做出单独的请求,那就是实际进行签名,也许是使用scribe来简化它......粗略地说:

        final OAuthService myTwitterService = TwitterClient.getTwitterClient().getService();
        final OAuthRequest aNiceOAuthRequest = new org.scribe.model.OAuthRequest(
                YOURPOST, THATURL);
Run Code Online (Sandbox Code Playgroud)

等等

第二次尝试:

但是,让我们不要这么做 - 结果你得到了最新版本的twitter4j.对不起首先走向死胡同 -我不应该假设,但是如果他们需要,我已经将上述内容包括在内,以帮助其他任何人.

事实证明,最新版本已在此处实施此端点文档.除了它需要一个StatusUpdate对象.所以你想做的事情如下:

final StatusUpdate statusUpdate = new StatusUpdate("Hallee hallo my status java.lang.String here...");
       // now do as you did until:
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
                 imageBytes = baos.toByteArray();
                 encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
       // then flip the stream
       byte[] myTwitterUploadBytes = bos.toByteArray();
       ByteArrayInputStream bis = new ByteArrayInputStream(myTwitterUploadBytes);
       // doo and double check your encoding etc, similar to in your question..
       statusUpdate.setMedia("give me a java.lang.String name", bis);
       // then continue just using twitter for j- update status as you would
       //... get twitter etc...
       //
       twitter4j.Status response = twitter.updateStatus(statusUpdate);
Run Code Online (Sandbox Code Playgroud)

目前还没有一个盒子让我测试 - 应该是正确的.如果仍然给出404,那么响应中的错误代码是什么?你认证了吗?

如果这不起作用,我们也可以尝试上面的一些作为备份.

希望这可以帮助,

最好,

汤姆.