2012-03-19 119 views
8

編輯:我想我已經想出瞭如何做二進制數據部分。在代碼中仔細檢查它,但我很肯定我的答案是正確的。新增的.debug()到OAuthService和更新的輸出:現在我想作爲Vimeo API documentationHTTP PUT上傳Java文件

編輯2描述完成上傳時,得到一個新的錯誤。

原問題:我正在嘗試使用Vimeo API (Streaming Method)將視頻上傳到Vimeo。我正在使用scribe來授權我的應用程序,獲取我的訪問令牌並準備上傳視頻。我只是不知道如何在Vimeo的API文檔說點做的PUT HTTP請求的這個例子中「文件的二進制數據,這裏」:

PUT http://1.2.3.4:8080/upload?ticket_id=abcdef124567890 HTTP/1.1

主持人:1.2.3.4:8080

的Content-Length:339108

的Content-Type:視頻/ MP4

....在這裏您文件的二進制數據....

我可以買票和標題罰款。只是,我該如何插入我的文件的二進制數據?

注:

  • 這是一個Java桌面應用
  • 這是一個小的視頻文件,我測試

這裏是我的代碼中put(記住,我使用scribe

// Setup File (line 52) 
File testUp = new File("C:/Users/Kent/Desktop/test.mp4"); 
String contentLength = Long.toString(testUp.length()); 
System.out.println("The content length is: " + contentLength); 
byte[] fileBytes = ByteStreams.toByteArray(new FileInputStream(testUp)); 

// Upload file (line 58) 
request = new OAuthRequest(Verb.PUT, endpoint); 
request.addHeader("Content-Length", contentLength); 
request.addHeader("Content-Type", "video/mp4"); 
request.addPayload(fileBytes); 
response = signSendAndPrint(service, accessToken, request, "Upload PUT: " + endpoint); 

// Check response code is valid (line 65) 
if (response.getCode() != 200) { 
    System.out.println("The response was not 200! It was: " + response.getCode()); 
    return; 
} 

// Verify the upload (line 71) 
request = new OAuthRequest(Verb.PUT, endpoint); 
request.addHeader("Content-Length", "0"); 
request.addHeader("Content-Range", "bytes */*"); 
response = signSendAndPrint(service, accessToken, request, "Verify Upload PUT: " + endpoint); 

// Check response code is valid (line 77) 
if (response.getCode() != 308) { 
    System.out.println("The response was not 308! It was: " + response.getCode()); 
    return; 
} 

// Complete Upload (line 83) 
request = new OAuthRequest(Verb.PUT, endpoint); 
request.addQuerystringParameter("method", "vimeo.videos.upload.complete"); 
Response completeResponse = signSendAndPrint(service, accessToken, request, "complete"); // This is line 86, it's the second to top level of my code that breaks the process. 

//Set video info (line 88) 
setVimeoVideoInfo(completeResponse, service, accessToken, vimeoAPIURL); 
    } 



/** 
    * Signs the request on the service, prints information on the request, sends the request, prints 
    * information on the response and returns the response 
    * 
    * @param service 
    * @param accessToken 
    * @param request 
    * @param description 
    * @return 
    */ 
    private static Response signSendAndPrint(OAuthService service, Token accessToken, OAuthRequest request, String description) { 
    service.signRequest(accessToken, request); 
    printRequest(request, description + " Request"); 
    Response response = request.send(); //This is line 105. It's the top level of my code that breaks the process 
    printResponse(response, description + " Response"); 
    return response; 
    } 

輸出的signSendAndPrint(OAuthService service, Token accessToken, OAuthRequest request, String description)方法和上OAuthService.debug()

Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Request 
Headers: {Authorization=OAuth oauth_signature="MTPIVFfGVUQn4QswNV6av4CjzJw%3D", oauth_version="1.0", oauth_nonce="-606493399", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428103", Content-Length=15125120, Content-Type=video/mp4} 
Verb: PUT 
Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e 

Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Response 
Code: 200 
Headers: {null=HTTP/1.1 200 OK, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0} 
Body: 
signing request: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e 
setting token to: Token[87bddf1382ac9f423d4b7c4166bdf0b2 , fdae7a033c7e1c932abce533627d6045124e8593] 
generating signature... 
base string is: PUT&http%3A%2F%2F174.129.155.54%3A8080%2Fupload&oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D1585934110%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e 
signature is: bhWu7IX9JKEEn/ULcpJECEuwqOc= 
appended additional OAuth parameters: { oauth_signature -> bhWu7IX9JKEEn/ULcpJECEuwqOc= , oauth_version -> 1.0 , oauth_nonce -> 1585934110 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MY_CONSUMER_KEY, oauth_token -> MY_OAUTH_TOKEN, oauth_timestamp -> 1332428110 } 
using Http Header signature 

Verify Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Request 
Headers: {Authorization=OAuth oauth_signature="bhWu7IX9JKEEn%2FULcpJECEuwqOc%3D", oauth_version="1.0", oauth_nonce="1585934110", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428110", Content-Length=0, Content-Range=bytes */*} *///Note, this is not part of the output, I just added */// so the rest of it doesn't appear commented out. 
Verb: PUT 
Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e 

Verify Upload PUT: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e Response 
Code: 308 
Headers: {null=HTTP/1.1 308 Resume Incomplete, Range=bytes=0-15125119, Content-Length=0, Connection=close, Content-Type=text/plain, Server=Vimeo/1.0} 
Body: 
signing request: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete 
setting token to: Token[87bddf1382ac9f423d4b7c4166bdf0b2 , fdae7a033c7e1c932abce533627d6045124e8593] 
generating signature... 
base string is: PUT&http%3A%2F%2F174.129.155.54%3A8080%2Fupload&method%3Dvimeo.videos.upload.complete%26oauth_consumer_key%3DMY_CONSUMER_KEY%26oauth_nonce%3D3111236130%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1332428110%26oauth_token%3DMY_OAUTH_TOKEN%26oauth_version%3D1.0%26ticket_id%3Da64ed67b4aefdc35d18aec6cfa0b7c5e 
signature is: vXlQ6OUKms8eHan+wEBO2HXBn/M= 
appended additional OAuth parameters: { oauth_signature -> vXlQ6OUKms8eHan+wEBO2HXBn/M= , oauth_version -> 1.0 , oauth_nonce -> 3111236130 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> MY_CONSUMER_KEY, oauth_token -> MY_OAUTH_TOKEN, oauth_timestamp -> 1332428110 } 
using Http Header signature 

complete Request 
Headers: {Authorization=OAuth oauth_signature="vXlQ6OUKms8eHan%2BwEBO2HXBn%2FM%3D", oauth_version="1.0", oauth_nonce="3111236130", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="MY_CONSUMER_KEY", oauth_token="MY_OAUTH_TOKEN", oauth_timestamp="1332428110"} 
Verb: PUT 
Complete URL: http://174.129.155.54:8080/upload?ticket_id=a64ed67b4aefdc35d18aec6cfa0b7c5e&method=vimeo.videos.upload.complete 
Exception in thread "main" org.scribe.exceptions.OAuthException: Problems while creating connection. 
    at org.scribe.model.Request.send(Request.java:70) 
    at org.scribe.model.OAuthRequest.send(OAuthRequest.java:12) 
    at autouploadermodel.VimeoTest.signSendAndPrint(VimeoTest.java:105) 
    at autouploadermodel.VimeoTest.main(VimeoTest.java:86) 
Caused by: java.net.SocketException: Unexpected end of file from server 
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:723) 
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589) 
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:720) 
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:589) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1319) 
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468) 
    at org.scribe.model.Response.<init>(Response.java:28) 
    at org.scribe.model.Request.doSend(Request.java:110) 
    at org.scribe.model.Request.send(Request.java:62) 
    ... 3 more 
Java Result: 1 

回答

2

完成上傳時需要附加兩個其他參數。 Vimeo API documentation表示您需要vimeo.videos.upload.complete的三個參數:文件名,oauth_token和ticket_id。抄寫員爲你照顧oauth_token。

此外,您必須調用原始端點,而不是請求票證時獲得的端點。

+0

太棒了!完美的作品!非常感謝你。我不敢相信這很簡單,但這是一半的時間。再次感謝 :) – kentcdodds 2012-03-22 16:02:05