Вопрос

Можно ли загружать изображения с помощью Scribe-Java и Twitter Post URL "https://upload.twitter.com/1/statuses/update_with_media.json"?
Мой источник
Я получаю ответ: {«Запрос»: «\ / 1 \ / States \ / update_with_media.json», «Ошибка»: «Не удалось аутентифицироваться с 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();
    }
.

Тренование здесь, конечно, добавляет размер 2 банок на ваш APK

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top