我正在开发一项服务,将视频上传到YouTube,我开始使用V2,但我正在迁移到V3,但我找不到要发送的REST端点。
可恢复上传是well docummented here,但直接上传仅记录为a Python script reference。
我的进程无法支持Resumable Uploads,因此我无法使用Java Client Library,因为我没有本地存储的文件。对于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);
答案 0 :(得分:0)
直接上传文档的等效文件,只需设置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