在Android上上传Twitter视频失败



我正在尝试在Android上上传视频到twitter。

每次使用API时,我都会在APPEND命令上获得HTTP 400(没有错误消息)。

我的请求看起来像这样(使用httpbin)

HEADERS
User-Agent: okhttp/3.2.0
Content-Length: 4000810
Total-Route-Time: 0
Accept-Encoding: gzip
Cf-Ipcountry: JP
Connection: close
Authorization: OAuth oauth_consumer_key="[Redacted]", oauth_nonce="[Redacted]", oauth_signature="[Redacted]%2BkY9kybcE%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1476240797", oauth_token="-[Redacted]", oauth_version="1.0"
Host: requestb.in
Content-Type: multipart/form-data; boundary=81302723-6d1b-4c0b-898a-ca52dd2aef10
Cf-Connecting-Ip: 118.238.220.243
X-Request-Id: e68c732e-5b59-4671-823a-f2ef1aa4e5c7
Via: 1.1 vegur
Cf-Visitor: {"scheme":"http"} => https for the real one
Connect-Time: 0
Cf-Ray: 2f0742ba47e0132f-NRT
RAW BODY
--81302723-6d1b-4c0b-898a-ca52dd2aef10
Content-Disposition: form-data; name="command"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 8
"APPEND"
--81302723-6d1b-4c0b-898a-ca52dd2aef10
Content-Disposition: form-data; name="media_id"
Content-Transfer-Encoding: binary
Content-Type: application/json; charset=UTF-8
Content-Length: 20
"[REDACTED]"
--81302723-6d1b-4c0b-898a-ca52dd2aef10
Content-Disposition: form-data; name="media"
Content-Transfer-Encoding: binary
Content-Type: video/avc
[Redacted binary gibberish]

我在使用API时得到的错误是:

反应{协议= h2,代码= 400,消息=,url = https://upload.twitter.com/1.1/media/upload.json}

最终文件大小为6mb;和ffmpeg -i on the file yeild the result

元数据:Major_brand: mp42Minor_version: 0compatible_brands: isommp42create_time: 2016-10-12 02:21:19版本:6.0持续时间:00:00:22.12,起始:0.000000,比特率:2387 kb/s流#0:0(eng):视频:h264(约束基线)(avc1/0x31637661), yuv420p, 480 × 480, 1135 kb/s, SAR 1:1: DAR 1:1, 25 fps, 25Tbr, 90k tbn, 50tbc(默认)元数据:create_time: 2016-10-12 02:21:19handler_name: VideoHandle

处理上传的代码如下:
class MPTwitterApiClient extends TwitterApiClient {
    public MPTwitterApiClient(TwitterSession session) {
        super(session);
    }
    /**
     * Provide CustomService with defined endpoints
     */
    public VideoService getVideoService() {
        return getService(VideoService.class);
    }
}
// example users/show service endpoint
interface VideoService {

    @FormUrlEncoded()
    @POST("https://upload.twitter.com/1.1/media/upload.json")
    Call<VideoUploadInit> uploadVideoInit(@Field("command") String command,
                                 @Field("total_bytes") String totalBytes,
                                 @Field("media_type") String mediaType);
    @Multipart
    @POST("https://upload.twitter.com/1.1/media/upload.json")
    Call <VideoUploadPart>uploadVideoAppend(@Part("command") String command,
                           @Part("media_id") String mediaId,
                           @Part("media") RequestBody media, // The raw binary file content being uploaded. Cannot be used with media_data.
                           // Required after an INIT, an index number starting at zero indicating the order of the uploaded chunks.
                           // The chunk of the upload for a single media, from 0-999, inclusive.
                           // The first segment_index is 0, the second segment uploaded is 1, etc.
                           @Part("segment_index") String segmentIndex);
    @POST("https://upload.twitter.com/1.1/media/upload.json")
      @FormUrlEncoded()
    Call<VideoUploadEnd>  uploadVideoFinalize(@Field("command") String command,
                             @Field("media_id") long mediaId);
    public class VideoUploadInit {
        @SerializedName("media_id")
        public final long mediaId;
        public VideoUploadInit(final long pMediaId) {
            mediaId = pMediaId;
        }
    }
    public class VideoUploadPart {
    }
    public class VideoUploadEnd {
    }
}

上传代码:

 private void uploadChunk(final VideoService videoService, final byte data[], final long mediaId , final int fileSize, final int chunkPart) {
        final int maxChunk = 4 * 1000 * 1000;
        final int byteSent = chunkPart * maxChunk;
        final boolean isLast = byteSent + maxChunk >= fileSize;
        RequestBody body = new RequestBody() {
            @Override
            public MediaType contentType() {
                return MediaType.parse("video/mp4");
            }
            @Override
            public void writeTo(final BufferedSink sink) throws IOException {
                sink.write(data, byteSent, isLast ? fileSize - byteSent : maxChunk);
            }
        };
        videoService.uploadVideoAppend("APPEND", String.valueOf(mediaId), body, String.valueOf(chunkPart)).enqueue(new Callback<VideoService.VideoUploadPart>() {
            @Override
            public void success(final Result result) {
                Log.d(TAG, "Uploaded video part");
                if (isLast) {
                    videoService.uploadVideoFinalize("FINALIZE", mediaId).enqueue(new Callback<VideoService.VideoUploadEnd>() {
                        @Override
                        public void success(final Result<VideoService.VideoUploadEnd> result) {
                            Log.e(TAG, "Finalized upload !");
                        }
                        @Override
                        public void failure(final TwitterException exception) {
                            Log.e(TAG, "Failed upload finalization");
                        }
                    });
                } else {
                    uploadChunk(videoService, data, mediaId, fileSize, chunkPart + 1);
                }
            }
            @Override
            public void failure(final TwitterException exception) {
                Log.e(TAG, "Could not upload video: " + exception);
            }
        });
    }

    private void tweet(TwitterSession pTwitterSession) {
        TwitterAuthToken authToken = pTwitterSession.getAuthToken();
        String token = authToken.token;
        String secret = authToken.secret;
        MPTwitterApiClient twitterApiClient = new MPTwitterApiClient(pTwitterSession);
        final VideoService videoService = twitterApiClient.getVideoService();
        final File videoFile = new File(AssetsUtils.getExportMoviePath(getApplicationContext()));
        Log.d(TAG, "File Size is " + (videoFile.length() / 1024 / 1024) + "mb");
        try {
            final byte data[] = Files.toByteArray(videoFile);
            videoService.uploadVideoInit("INIT", String.valueOf(videoFile.length()), "video/mp4").enqueue(new Callback<VideoService.VideoUploadInit>() { //XXX refactor this callback hell
                @Override
                public void success(final Result<VideoService.VideoUploadInit> result) {
                    Log.d(TAG, "Succeed INIT RESULT: " +result);
                    Log.d(TAG, "media ID is " + result.data.mediaId);
                    final long mediaId = result.data.mediaId;
                    final int fileSize = (int)videoFile.length();
                    uploadChunk(videoService, data, mediaId, fileSize, 0);
                    }
                @Override
                public void failure(final TwitterException exception) {
                    Log.d(TAG, "Failed twitter init with " + exception);
                }
            });
        } catch (IOException pE) {
            pE.printStackTrace();
        }
    }

media INIT call运行成功并返回一个media id。追加部分在第一个块上失败。

(是的,它是全白色的,因为内容仍然处于NDA之下)。

好了,我终于想通了。

请求的格式不符合twitter的要求。我更新了这段代码(你需要一点重构)。

private void uploadChunk(final VideoService videoService, final byte data[], final long pMediaId , final int fileSize, final int chunkPart) {
    final int maxChunk = 4 * 1000 * 1000;
    final int byteSent = chunkPart * maxChunk;
    final boolean isLast = byteSent + maxChunk >= fileSize;
    RequestBody body = new RequestBody() {
        @Override
        public MediaType contentType() {
            return MediaType.parse(Encoder.MIME_TYPE);
        }
        @Override
        public void writeTo(final BufferedSink sink) throws IOException {
            sink.write(data, byteSent, isLast ? fileSize - byteSent : maxChunk);
        }
    };
    final RequestBody mediaIdBody = new RequestBody() {
        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain");
        }
        @Override
        public void writeTo(final BufferedSink sink) throws IOException {
            sink.writeString(String.valueOf(pMediaId), Charset.defaultCharset());
        }
    };
    RequestBody appendCommand = new RequestBody() {
        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain");
        }
        @Override
        public void writeTo(final BufferedSink sink) throws IOException {
            sink.writeString("APPEND", Charset.defaultCharset());
        }
    };
    RequestBody chunkPartBody = new RequestBody() {
        @Override
        public MediaType contentType() {
            return MediaType.parse("text/plain");
        }
        @Override
        public void writeTo(final BufferedSink sink) throws IOException {
            sink.writeString(String.valueOf(chunkPart), Charset.defaultCharset());
        }
    };
    videoService.uploadVideoAppend(appendCommand, mediaIdBody, body, chunkPartBody).enqueue(new Callback<VideoService.VideoUploadPart>() {
        @Override
        public void success(final Result result) {
            Log.d(TAG, "Uploaded video part");
            if (isLast) {
                videoService.uploadVideoFinalize("FINALIZE", pMediaId).enqueue(new Callback<VideoService.VideoUploadEnd>() {
                    @Override
                    public void success(final Result<VideoService.VideoUploadEnd> result) {
                        Log.e(TAG, "Finalized upload !");
                    }
                    @Override
                    public void failure(final TwitterException exception) {
                        Log.e(TAG, "Failed upload finalization");
                    }
                });
            } else {
                uploadChunk(videoService, data, pMediaId, fileSize, chunkPart + 1);
            }
        }
        @Override
        public void failure(final TwitterException exception) {
            Log.e(TAG, "Could not upload video: " + exception);
        }
    });
}

在twitter api客户端:

@Multipart
@POST("https://upload.twitter.com/1.1/media/upload.json")
Call <VideoUploadPart>uploadVideoAppend(@Part("command") RequestBody command,
                       @Part("media_id") RequestBody mediaId,
                       @Part("media") RequestBody media, // The raw binary file content being uploaded. Cannot be used with media_data.
                       // Required after an INIT, an index number starting at zero indicating the order of the uploaded chunks.
                       // The chunk of the upload for a single media, from 0-999, inclusive.
                       // The first segment_index is 0, the second segment uploaded is 1, etc.
                       @Part("segment_index") RequestBody segmentIndex);

最新更新