I am working on an app in which the user can post certain things to Twitter and can also take a picture to add to a Tweet. I am new to Fabric and Retrofit, so I have some questions concerning how to implement the image upload and how to use the uploaded image with the Statuses Service's update function. (I am aware that TweetComposer allows to post images with tweets easily, but I don't want the additional Twitter UI element and I also want to be able to set the location associated with the tweet programatically, which is not possible with TweetComposer).
So far what I have understood that in order to include images in a tweet posted using the Statuses Service update method, we need to upload the image itself first to Twitter. I have found that uploading images through the REST API is done by using this URL:
https://upload.twitter.com/1.1/media/upload.json
I have also found that it is possible to customise the Twitter API client provided in Fabric to access service endpoints.
class MyTwitterApiClient extends TwitterApiClient {
public MyTwitterApiClient(TwitterSession session) {
super(session);
}
/**
* Provide CustomService with defined endpoints
*/
public CustomService getCustomService() {
return getService(CustomService.class);
}
}
// example users/show service endpoint
interface CustomService {
@GET("/1.1/users/show.json")
void show(@Query("user_id") long id, Callback<User> cb);
}
However, I would be glad to get some more explanation. First of all, how do I change the domain (to upload.twitter.com) in the API client and what type should I expect in the callback?
Also, if the image is uploaded successfully and I manage to get its ID, how can I use it to attach this media object to a tweet created by the Statuses Service's update function? I haven't found any way in the documentation to attach entities.
So far I have this:
public class MyTwitterApiClient extends TwitterApiClient {
public MyTwitterApiClient(TwitterSession session)
{
super(session);
}
public UploadMediaService getUploadMediaService() {
return getService(UploadMediaService.class);
}
}
interface UploadMediaService {
@Multipart
@POST("1.1/media/upload.json")
void upload(@Part("media") TypedFile file, @Part("additional_owners") String owners, Callback cb);
}
Is there anyone more knowledgeable who could give me some more guidance? Thanks in advance!
Finally, in the latest version of Twitter Kit support has been added for media upload and for tweeting with media IDs (using the StatusesService).
I have been able to tweet with picture using StatusesService by adding the code below:
File photo = new File(mCurrentPhotoPath);
TypedFile typedFile = new TypedFile("application/octet-stream", photo);
MediaService ms = twitterclient.getMediaService();
ms.upload(typedFile, null, null, new Callback<Media>() {
@Override
public void success(Result<Media> mediaResult) {
StatusesService statusesService = TwitterCore.getInstance().getApiClient(session).getStatusesService();
statusesService.update("@##### " + eventtype + " lähellä paikkaa: " + place.getText().toString() + ";\n" + comment.getText().toString(), null, false, mypos.latitude, mypos.longitude, null, true, false, mediaResult.data.mediaIdString, new Callback<Tweet>() {
@Override
public void success(Result<Tweet> tweetResult) {
//...
}
@Override
public void failure(TwitterException e) {
//...
}
});
}
@Override
public void failure(TwitterException e) {
//...
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With