Pergunta

É possível fazer upload de imagens usando Scribe-Java e Twitter POST Url "https://upload.twitter.com/1/statuses/update_with_media.json"?
minha fonte
Eu recebo a resposta:{"solicitar":"\ / 1 \ / status \ / update_with_media.json", "erro":"Não foi possível autenticar com OAuth."}

Foi útil?

Solução

Apenas para ajudar qualquer pessoa que esteja olhando para isso, uma maneira simples de construir a parte múltipla é adicionar httpmime-4.0.1.jar e apache-mime4j-0.6.jar ao seu caminho e fazer o seguinte.

        /* 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();
    }

A desvantagem aqui é, obviamente, adicionar o tamanho dos 2 frascos ao seu APK

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top