通过Android使用ClientLogin直接上传YouTube
我必须制作一个为特定YouTube用户上传视频的应用程序。我尝试了几个库,但不幸的是没有成功。所以,由于我想做的事情非常简单,我决定自己做,但直到现在运气都不好。我能够成功检索身份验证令牌,但无法使文件上载正常工作。这是我的密码:通过Android使用ClientLogin直接上传YouTube,android,youtube,http-post,Android,Youtube,Http Post,我必须制作一个为特定YouTube用户上传视频的应用程序。我尝试了几个库,但不幸的是没有成功。所以,由于我想做的事情非常简单,我决定自己做,但直到现在运气都不好。我能够成功检索身份验证令牌,但无法使文件上载正常工作。这是我的密码: File file = new File(videoPath); String boundary = "f93dcbA3"; String endLine = "\r\n"; StringBuilder sb = new StringBui
File file = new File(videoPath);
String boundary = "f93dcbA3";
String endLine = "\r\n";
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(boundary);
sb.append(endLine);
sb.append("Content-Type: application/atom+xml; charset=UTF-8");
sb.append(endLine);
sb.append(endLine);
sb.append(entry);
sb.append(endLine);
sb.append("--");
sb.append(boundary);
sb.append(endLine);
sb.append("Content-Type: video/3gpp");
sb.append(endLine);
sb.append("Content-Transfer-Encoding: binary");
sb.append(endLine);
sb.append(endLine);
String bodyStart = sb.toString();
sb = new StringBuilder();
sb.append(endLine);
sb.append("--");
sb.append(boundary);
sb.append("--");
String bodyEnd = sb.toString();
HttpURLConnection conn;
try {
FileInputStream fIn = new FileInputStream(file);
byte fileBytes[] = new byte[(int) file.length()];
fIn.read(fileBytes);
conn = (HttpURLConnection) new URL("http://uploads.gdata.youtube.com/feeds/api/users/" + username + "/uploads")
.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/related; boundary=\"" + boundary + "\"");
conn.setRequestProperty("Host", "uploads.gdata.youtube.com");
conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth);
conn.setRequestProperty("GData-Version", "2");
conn.setRequestProperty("X-GData-Key", "key=" + devKey);
conn.setRequestProperty("Slug", "video.3gp");
conn.setRequestProperty("Content-Length", "" + (bodyStart.getBytes().length
+ fileBytes.length + bodyEnd.getBytes().length));
conn.setRequestProperty("Connection", "close");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
try {
conn.connect();
Log.d("ID", "" + file.length());
try {
OutputStream os = new BufferedOutputStream(conn.getOutputStream());
os.write(bodyStart.getBytes());
os.write(fileBytes);
os.write(bodyEnd.getBytes());
os.flush();
String response = "";
try {
response = "Success! " + read(conn.getInputStream());
} catch (FileNotFoundException e) {
// Error Stream contains JSON that we can parse to a FB error
response = "Error!" + read(conn.getErrorStream());
}
Log.d("ID", response);
} catch (FileNotFoundException e1) {
Log.d("ID", e1.getMessage(), e1);
} catch (IOException e) {
Log.d("ID", e.getMessage(), e);
}
} catch (IOException e2) {
Log.d("ID", e2.getMessage(), e2);
}
} catch (MalformedURLException e3) {
Log.d("ID", e3.getMessage(), e3);
} catch (IOException e3) {
Log.d("ID", e3.getMessage(), e3);
}
以及xml条目(来自规范的简单复制粘贴):
private static String entry=“”
+ ""
+ ""
+“糟糕的婚礼祝酒词”
+ ""
+“我在朋友的婚礼上做了一个糟糕的祝酒词。也许吧”
+ ""
+“人”
+ ""
+“敬酒,婚礼”
+ ""
+ "";
上面的代码是根据
简而言之,我收到的响应是“411长度要求”,这意味着我在标题中没有内容长度,但我有!我不知道问题出在哪里,我不知道我的代码是否正确:(
如果您能给我提供任何帮助,我将不胜感激:(我是在Wireshark的帮助下找到的!虽然我仍然看不到它,但在我的身份验证字符串的末尾有一个新行符号。这在我的http头中添加了一个额外的行,该行不应该在那里,最后出于某种奇怪的原因,结果是“411长度必需”
因此,如果您遇到相同的错误,请尝试删除身份验证字符串中的最后一个字符。我是在Wireshark的帮助下找到的!虽然我仍然看不到它,但在身份验证字符串的末尾有一个新行符号。这会在我的http标头中添加一个不应该存在的额外行,最后的结果是“411所需长度”出于某种奇怪的原因 因此,如果出现相同的错误,请尝试从auth字符串中删除最后一个字符
private static String entry = "<?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\">Bad Wedding Toast</media:title>"
+ "<media:description type=\"plain\">"
+ "I gave a bad toast at my friend's wedding. Maybe"
+ "</media:description>"
+ "<media:category"
+ " scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">People"
+ "</media:category>"
+ "<media:keywords>toast, wedding</media:keywords>"
+ "</media:group>"
+ "</entry>";