2012-05-21 107 views
2

是否可以使用Scribe-Java和twitter POST Url上傳圖片「https://upload.twitter.com/1/statuses/update_with_media.json」?
my source
我得到的迴應:{ 「請求」: 「\/1 \ /狀態\/update_with_media.json」, 「錯誤」: 「無法與OAuth的認證。」}Twitter。發佈媒體狀態

+0

我收到了來自開發商的回答:「這是可能的,但你需要創建一個多HTTP請求」 –

+0

你使用抄寫員OauthRequest得到這個工作還是你採取了不同的方法? –

回答

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

+0

這很好,謝謝! – afollestad