I’m building a Twitter client for Android. The HTTP client I’m using is Retrofit. As of lately, I’ve started implementing the chunked media upload. When testing, I found out that the request to the media/upload (INIT) endpoint always fails with code 400 (which is bad request) and returns me the following response:
{“request”:"/1.1/media/upload.json",“error”:“Invalid command: Some(“INIT”).”}
The error says that the command (which in this request is INIT) is invalid, although I’ve checked multiple times and it seems fine to me. Also, I’ve searched the forums for similiar erros, but couldn’t find any. I don’t think this error is connected with authentication header because other requests work perfectly. Any help would be much appreciated. Code attached below.
Retrofit request:
@Multipart
@POST
public Call<MediaUploadResponse> initMediaUpload(@Header("Authorization") String authorizationHeader,
@Url String url,
@Part("command") String command,
@Part("total_bytes") long totalBytes,
@Part("media_type") String mediaType,
@Part("media_category") String mediaCategory,
@Part("additional_owners") String additionalOwners);
The function performing the request:
public static MediaUploadResponse initMediaUpload(OAuthCredentials authorizationCredentials, Media media) {
TwitterService service = AppController.getInstance().getTwitterService();
//creating the authorization header
String authorizationHeader = createAuthorizationHeader(
Method.POST,
TWITTER_UPLOAD_MEDIA_ENDPOINT,
authorizationCredentials.accessToken,
authorizationCredentials.accessTokenSecret,
"",
null,
false
);
Call<MediaUploadResponse> apiCall = service.initMediaUpload(
authorizationHeader,
TWITTER_UPLOAD_MEDIA_ENDPOINT,
"INIT",
Utils.getImageSizeFromPath(media.getMediaUrl()),
Utils.getMimeType(media.getMediaUrl()),
null,
null
);
MediaUploadResponse mediaUploadResponse = null;
try {
Response<MediaUploadResponse> response = apiCall.execute();
// Now the apiCall is executed, but the body of the response is null while the errorBody tells me about the aforementioned error
if((response != null) && (response.isSuccessful()) && (response.body() != null)) {
mediaUploadResponse = response.body();
}
} catch(IOException e) {
if(Constants.IS_IN_DEBUG_MODE) {
Log.e(TAG, "An error occurred while initializing media upload. Error: " + e.getLocalizedMessage());
}
}
return mediaUploadResponse;
}