2014-01-13 54 views
1

我正在開發一項服務以將視頻上傳到YouTube,並且我在V2上工作,但我正在遷移到V3,但我找不到REST端點打。將視頻上傳到YouTube數據API v3而不使用Java客戶端庫

可恢復上傳爲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); 

回答

0

直接上傳相當於文檔,只是設置uploadType = direct。

您可以在此示例中將directUploadEnabled設置爲true,並查看發佈請求以確認。 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

+0

這仍然使用Java客戶端API,這就是我想要避免的。 – avillagomez

+0

我問過你,只需創建一個演示並找到發佈請求。這樣你就可以用相同的選項建立你的手動發佈請求。還更新了我的答案。 –

+0

謝謝,我會嘗試一下,我會在找到合適的解決方案後進行投票。 – avillagomez

相關問題