是否可以使用 Scribe-Java 和 twitter POST Url“https://upload.twitter.com/1/statuses/update_with_media.json”上传图像?
我的消息来源
我得到了响应: {"request": "\ / 1 \ / statuses \ / update_with_media.json", "error": "Could not authenticate with OAuth."}
问问题
1410 次
1 回答
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
于 2012-07-02T15:45:28.287 回答