推特。发布媒体状态

时间:2012-05-21 13:23:47

标签: android twitter scribe

是否可以使用Scribe-Java和twitter POST Url" https://upload.twitter.com/1/statuses/update_with_media.json"上传图像?
my source
我得到了回复:{"请求":" \ / 1 \ / statuses \ / update_with_media.json","错误":"无法验证与OAuth。"}

1 个答案:

答案 0 :(得分:3)

只是为了帮助其他人看这个,构建多部分的一个简单方法是将httpmime-4.0.1.jar和apache-mime4j-0.6.jar添加到您的路径中并执行以下操作。

        /* You will have done this bit earlier to authorize the user

    OAuthService service = new ServiceBuilder().provider(TwitterApi.SSL.class).apiKey("[YOUR API KEY]").apiSecret("[YOUR SECRET]").callback("twitter://callback").build();
    Token accessToken = Do you oauth authorization as normal 

    */  

    OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json");
    MultipartEntity entity = new MultipartEntity();
    try {

        entity.addPart("status", new StringBody("insert vacuous statement here"));
        entity.addPart("media", new FileBody(new File("/path/of/your/image/file")));

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        entity.writeTo(out);

        request.addPayload(out.toByteArray());
        request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());

        service.signRequest(accessToken, request);
        Response response = request.send();

        if (response.isSuccessful()) {
            // you're all good
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

这里的权衡当然是将2个罐子的大小添加到你的APK