Help: (Does anyone know how to write a POST request to download a Youtube video through the Youtube API, in Java?
The structure of the requested POST request is described here .
Unfortunately, I cannot use the Youtube client libraries for Java, because I work on Android that does not support them.
I have several code attempts below, but it does not work (I get an error that conn.getResponseCode () is null).
public void videoUpload() {
HttpURLConnection conn = null;
DataOutputStream dos = null;
InputStream inStream = null;
String path = "file:///sdcard/";
String existingFileName = "video.3gp";
File videoFile = new File(path + existingFileName);
String twoHyphens = "--";
String boundary = "b93dcbA3";
String ver = "2";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1024;
try {
FileInputStream fileInputStream = new FileInputStream(videoFile);
URL url = new URL("http://uploads.gdata.youtube.com/feeds/api/users/[XXXXXXXXXX]/uploads");
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Host", "uploads.gdata.youtube.com");
conn.setRequestProperty("Authorization", "GoogleLogin auth=" + token);
conn.setRequestProperty("GData-Version", ver);
conn.setRequestProperty("X-Gdata-Client", clientId);
conn.setRequestProperty("X-GData-Key", "key=" + developerKey);
conn.setRequestProperty("Slug", existingFileName);
conn.setRequestProperty("Content-Type",
"multipart/related;boundary=" + boundary);
conn.setRequestProperty("Content-Length", new Long(videoFile
.length()).toString());
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Content-Type",
"application/atom+xml; charset=UTF-8");
conn.setRequestProperty("Content-Type", "video/3gpp");
conn.setRequestProperty("Content-Transfer-Encoding", "binary");
dos = new DataOutputStream(conn.getOutputStream());
dos.write((twoHyphens + boundary).toString().getBytes());
StringBuilder test_xml = new StringBuilder();
test_xml.append("<?xml version='1.0' encoding='UTF-8'?>\n");
test_xml.append("<entry xmlns=\"http://www.w3.org/2005/Atom\"\n");
test_xml.append("xmlns:media=\"http://search.yahoo.com/mrss/\"\n");
test_xml.append("xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">\n");
test_xml.append("<media:group>\n");
test_xml.append("<media:title type=\"plain\">Test Video</media:title>\n");
test_xml.append("<media:description type=\"plain\">\nTest Video\n</media:description>\n");
test_xml.append("<media:category\n");
test_xml.append("scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">People\n");
test_xml.append("</media:category>\n");
test_xml.append("<media:keywords>toast, election, wedding</media:keywords>\n");
test_xml.append("</media:group>\n");
test_xml.append("</entry>");
dos.write(test_xml.toString().getBytes("UTF-8"));
dos.write((twoHyphens + boundary).toString().getBytes());
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.write((twoHyphens + boundary + twoHyphens).toString()
.getBytes());
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
int responseCode;
StringBuilder outputBuilder = new StringBuilder();
try {
responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = conn.getInputStream();
} else {
inStream = conn.getErrorStream();
}
String string;
if (inStream != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(inStream));
while (null != (string = reader.readLine())) {
outputBuilder.append(string).append("\n");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(outputBuilder.toString());
Log.d(LOG_TAG, outputBuilder.toString());
}
AP257 source
share