Code: Select all
public interface TwitterApiService {
@Multipart
@POST("1.1/media/upload.json")
Call uploadMedia(@Part MultipartBody.Part media);
}
Code: Select all
public String uploadMedia (String media) throws Exception {
String fileUrl = "https://ayxsaves.s3.eu-central,,,";
String destinationPath = "downloaded_image.jpg";
File file = ImageUtils.downloadFile(fileUrl, destinationPath);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("media", file.getName(), requestBody);
TwitterHeader twitterHeader = new TwitterHeader(headerGenerator);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(twitterHeader)
.addInterceptor(new HttpLoggingInterceptor())
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
TwitterApiService service = retrofit.create(TwitterApiService.class);
TwitterMediaResponse mediaResponse = service.uploadMedia(body).execute().body();
return mediaResponse.getMediaIdString();
}