質問

スクribe-JavaとTwitterを使って画像をアップロードすることは、URL "https://upload.twitter.com/1/statuses/update_with_media.json"を使ってアップロードすることができますか?
マイソース
{"request": "\ / 1 \ / statues \ / update_with_media.json"、 "error": "OAuthで認証できませんでした。}

役に立ちましたか?

解決

これを見ている人を見ているだけで、マルチパートを構築する簡単な方法は、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();
    }
.

ここでのトレードはもちろんあなたのAPKに2つのjarのサイズを追加することです

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top