Hi,
I’m using java’s framework spring social and i’m having difficulties to send direct messages with a picture attached to it.
I tried two methods :
- Copying the method used to send tweets :
Resource resource = new FileSystemResource("pathToPicture");
TweetData tweetData = new TweetData("whatever").withMedia(resource);
MultiValueMap<String, Object> postParameters = tweetData.toRequestParameters();
if (tweetData.hasMedia()) {
MediaUploadResponse response = twitter.restOperations().postForObject("https://upload.twitter.com/1.1/media/upload.json",
tweetData.toUploadMediaParameters(), MediaUploadResponse.class);
postParameters.set("media_ids", response.getMediaId());
postParameters.add("screen_name", "screen_name");
postParameters.add("text", "Test dm with picture");
}
return twitter.restOperations().postForObject("https://api.twitter.com/1.1/direct_messages/new.json", postParameters, String.class);
This seem to work as i get a JSON return without error, and messages ids but when i check the dm was indeed sent but the picture wasn’t.
- The second method was to do all the request following the POST request INIT, APPEND and FINALIZE.
All these POST request seem to work fine (i got the medi_id) but when i construct the final POST request to send the message i always get an error, i don’t know how to do it. This is this request :
MediaUploadResponse paramMediaId = new MediaUploadResponse();
MultiValueMap<String, Object> postParameters = tweetData.toRequestParameters();
postParameters.set("media_ids", paramMediaId.getMediaId());
postParameters.add("screen_name", "screen_name");
postParameters.add("text", "Test dm with picture");
return twitter.restOperations().postForObject("https://api.twitter.com/1.1/direct_messages/events/new.json", postParameters, String.class);
Note that MediaUploadResponse.class is a simple class created for that purpose :
@JsonIgnoreProperties(ignoreUnknown=true)
class MediaUploadResponse {
@JsonProperty("media_id")
private String mediaId;
/*public MediaUploadResponse(String mediaId){
this.mediaId = mediaId;
}*/
public String getMediaId() {
return mediaId;
}
}```
Thank you