我正在尝试使用 RequestBuilder 从我的 GWT 应用程序的客户端发送一个 POST 请求以从 YouTube 获取上传令牌。
我正在尝试按照以下 YouTube API 文档的描述构建请求: https ://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading#Browser_based_uploading
以下是我的客户端 GWT 代码:
// BODY
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\"?>"
+ "<entry xmlns=\"http://www.w3.org/2005/Atom\""
+ "xmlns:media=\"http://search.yahoo.com/mrss/\""
+ "xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">"
+ "<media:group>"
+ "<media:title type=\"plain\">MyTitle</media:title>"
+ "<media:description type=\"plain\">"
+ "My description."
+ "</media:description>"
+ "<media:category scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">"
+ "People"
+ "</media:category>"
+ "<media:keywords>word1, word2</media:keywords>"
+ "</media:group>"
+ "</entry>");
String requestBody = sb.toString();
// HEADER
RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, "https://" + YOUTUBE_HOST + "/action/GetUploadToken");
rb.setHeader("Host", YOUTUBE_HOST);
rb.setHeader("Content-Type", "application/atom+xml; charset=" + CHAR_ENCODING);
rb.setHeader("X-GData-Key", MY_CLIENT_KEY);
rb.setHeader("GData-Version", "2");
try {
rb.setHeader("Content-Length", Integer.toString(requestBody.getBytes(CHAR_ENCODING).length));
}
catch (UnsupportedEncodingException e) {
Window.alert(e.getMessage());
}
rb.setHeader("Authorization", "Bearer " + AUTHORIZATION_CODE);
// CALL
try {
rb.sendRequest(requestBody, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
Document dom = XMLParser.parse(response.getText());
Node urlNode = dom.getElementsByTagName("url").item(0);
String url = urlNode.getFirstChild().getNodeValue();
Node tokenNode = dom.getElementsByTagName("token").item(0);
String token = tokenNode.getFirstChild().getNodeValue();
}
@Override
public void onError(Request request, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
catch (RequestException e) {
callback.onFailure(e.getMessage());
}
以下是我在 Chrome 的开发者工具中显示的请求:
Request URL:https://gdata.youtube.com/action/GetUploadToken
Request Method:OPTIONS
Status Code:404 Not Found
请求标头
:host:gdata.youtube.com
:method:OPTIONS
:path:/action/GetUploadToken
:scheme:https
:version:HTTP/1.1
accept:*/*
accept-charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
accept-encoding:gzip,deflate,sdch
accept-language:en-GB,en-US;q=0.8,en;q=0.6
access-control-request-headers:origin, x-gdata-key, authorization, content-type, gdata-version
access-control-request-method:POST
origin:http://127.0.0.1:8888
referer:http://127.0.0.1:8888/MyApp.html?gwt.codesvr=127.0.0.1:9997
user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11
响应标头
cache-control:no-cache, no-store, max-age=0, must-revalidate
content-encoding:gzip
content-length:117
content-type:text/html; charset=UTF-8
date:Thu, 06 Dec 2012 14:29:20 GMT
expires:Fri, 01 Jan 1990 00:00:00 GMT
pragma:no-cache
server:GSE
status:404 Not Found
version:HTTP/1.1
x-content-type-options:nosniff
x-frame-options:SAMEORIGIN
x-gdata-user-country:US
x-xss-protection:1; mode=block
如上所示,我在发送请求后收到“404 Not Found”错误。
任何帮助将不胜感激!