我正在尝试发送POST请求,以便使用我的GWT应用客户端的RequestBuilder从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”错误。
非常感谢任何帮助!
答案 0 :(得分:2)
我对GWT并不是很熟悉,但我注意到了一些特殊的东西。事实上,Chrome报告的请求是OPTIONS
请求,而不是YouTube API要求的POST
。在向此URL发送任意OPTIONS
请求时,我能够重现404。
您的代码表明您尝试发送POST
请求。但是,我认为这是一个跨源请求,它将由Same Origin Policy管理。使用GWT查看this跨站点请求的示例。发送OPTIONS
请求而不是POST
请求的奇怪行为可能是由于代码将此处理为同一站点请求。
答案 1 :(得分:1)
YouTube API的CORS支持已经被打破了大约两个星期了。工程团队向我保证,它将很快,很快(希望今天)得到修复。这就是造成你所看到错误的原因。
您可以关注https://code.google.com/p/youtube-direct-lite/issues/detail?id=9了解更新。 (它主要影响YouTube Direct Lite,因为没有很多其他应用依赖CORS。)