java - 不使用 Java 客户端库将视频上传到 YouTube Data API v3

标签 java youtube youtube-api

我正在开发一项将视频上传到 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);

最佳答案

直接上传相当于文档,只需设置 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 - 不使用 Java 客户端库将视频上传到 YouTube Data API v3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103048/

相关文章:

javascript - 在 for 循环中查询 Youtube 的标题仅显示第一项

youtube - 在youtube的隐藏式字幕中搜索短语

java - android 使用服务或使用报警管理器

java - JDK 11 的哪个版本的 JRE?

android - 如何从一个 channel 中获得所有YouTube视频?

php - 您能帮我解决这个YouTube rss feed吗?

youtube - 如何激活adsense帐户?

Youtube API V3 和 Etag

java - Spring boot : java. lang.StackOverflowError:null 注册成功后尝试自动登录

java - 当我尝试打印所有组时,我得到 "IndexOutOfBoundsException: No group 4"