【问题标题】:Upload video to YouTube Data API v3 without using the Java Client Library不使用 Java 客户端库将视频上传到 YouTube 数据 API v3
【发布时间】:2014-02-01 21:25:53
【问题描述】:

我正在开发一项将视频上传到 YouTube 的服务,并且我在 V2 上运行它,但我正在迁移到 V3,但我找不到要访问的 REST 端点。

可恢复上传为well docummented here,但直接上传仅记录为a Python script reference

我的进程不支持可恢复上传,并且我无法使用 Java 客户端库,因为我没有本地存储文件。对于 V2,我使用了一种不同的方法来进行管道处理。

有没有办法在 V3 上做到这一点???

String lineEnd = "\r\n";
String twoHyphens = "--";

String access_token = "A1S2D3F4G5H6J7K8L9";
String gameClipID = "gameClipID";
String sourceServerURL = "sourceServerURL";

String boundary = "Boundary" + gameClipID;
String closingBoundary = lineEnd + twoHyphens + boundary + twoHyphens;

URL youTubeURL = new URL("http://uploads.gdata.youtube.com/feeds/api/users/default/uploads");
HttpURLConnection youTubeConnection = (HttpURLConnection)youTubeURL.openConnection();
youTubeConnection.setRequestMethod("POST");
youTubeConnection.setDoOutput(true);
youTubeConnection.setDoInput(true);

// Set Headers
youTubeConnection.setRequestProperty("Authorization" , "GoogleLogin auth=" + auth);
youTubeConnection.setRequestProperty("GData-Version", "2");
youTubeConnection.setRequestProperty("X-GData-Key", "key=" + developerKey);
youTubeConnection.setRequestProperty("Slug", gameClipID);
youTubeConnection.setRequestProperty("Accept", "*/*");
youTubeConnection.setRequestProperty("Accept-Charset", "UTF-8");
youTubeConnection.setRequestProperty("Content-Type", "multipart/related;boundary=" + boundary);

String content = prepareContent(boundary, "Title", "Description", keywords);
Long totalLength = (long)content.length() + (long)fileSize + (long)closingBoundary.length();

youTubeConnection.setRequestProperty("Content-Length", totalLength.toString());
youTubeConnection.setRequestProperty("Connection", "close");

URL sourceURL = new URL(sourceServerURL);
HttpURLConnection sourceConnection = (HttpURLConnection) sourceURL.openConnection();
sourceConnection.setAllowUserInteraction(false);
sourceConnection.setRequestProperty("Authorization", access_token);
sourceConnection.connect();

BufferedInputStream bis = new BufferedInputStream(sourceConnection.getInputStream());

BufferedOutputStream request = new BufferedOutputStream(youTubeConnection.getOutputStream());

request.write(content.getBytes("UTF-8"));

boolean eof = false;
byte[] input = new byte[4096];
while (!eof) {
    int length = bis.read(input);
    if (length == -1) {
        eof = true;
    } else {
        request.write(input, 0, length);
    }
}

request.write(closingBoundary.getBytes("UTF-8"));
request.flush();
request.close();
bis.close();
sourceConnection.disconnect();

InputStream responseStream = new BufferedInputStream(youTubeConnection.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));

String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
    stringBuilder.append(line);
}
responseStreamReader.close();

String response = stringBuilder.toString();
responseStream.close();
youTubeConnection.disconnect();

return YouTubeClient.parseXMLResponse(response);

【问题讨论】:

    标签: java youtube youtube-api


    【解决方案1】:

    直接上传相当于文档,只是设置uploadType=direct。

    您可以在此示例中将 directUploadEnabled 设置为 true,并查看 Post 请求以确保。 https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/UploadVideo.java#L136

    这里还有 directUpload 的源代码,您可以手动构建它。 https://code.google.com/p/google-api-java-client/source/browse/google-api-client/src/main/java/com/google/api/client/googleapis/media/MediaHttpUploader.java#345

    【讨论】:

    • 这仍在使用 Java 客户端 API,这是我试图避免的。
    • 我让你这样做,只需用它创建一个演示并找到发布请求。这样您就可以使用相同的选项构建手动发布请求。还更新了我的答案。
    • 谢谢,我会试试,找到合适的解决方案后我会投票。
    猜你喜欢
    • 2012-10-22
    • 2011-11-19
    • 1970-01-01
    • 2013-01-27
    • 2012-12-23
    • 1970-01-01
    • 2015-08-12
    • 2016-08-22
    • 1970-01-01
    相关资源
    最近更新 更多