我想将一些视频文件上传到我的YouTube。
我知道官方网站(https://developers.google.com/youtube/v3/docs/videos),但我使用的是android平台,但是没有android play服务。
然后我了解Google API客户端库。
示例使用com.google.api.services.youtube
。但我不能使用YouTube API。
我只是通过Java中的http上传了视频文件,而没有youtube api。
这是我的测试源。 注释(//这不起作用)正在发送视频。如果来源未评论,我收到了400条回复。如果源评论,我收到200答复。
public static String uploadMetaData(String filePath, boolean retry, String accessToken) throws IOException {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
//String baseURL = "https://www.googleapis.com/youtube/v3/videos";
String baseURL = "https://www.googleapis.com/upload/youtube/v3/videos";
String urlData = "uploadType=resumable&part=snippet,status,contentDetails";
String uploadUrl = baseURL + "?" + urlData;
String postDataForm = "{\n\"snippet\": {\n\"title\": \"%s\",\n\"description\": \"%s\",\n\"tags\": " + "[\"cool\", \"video\", \"more keywords\"],\n\"categoryId\": 22\n},\n\"status\": " + "{\n\"privacyStatus\": \"public\",\n\"embeddable\": True,\n\"license\": \"youtube\"\n}\n}";
String postData = String.format(postDataForm, "hong", "hong", "hong");
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(postData);
} catch (JSONException e) {
e.printStackTrace();
}
URL url = new URL(uploadUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Host", "www.googleapis.com");
urlConnection.setRequestProperty("Authorization", String.format("Bearer %s", accessToken));
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setRequestProperty("X-Upload-Content-Type", "video/*");
//urlConnection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
//urlConnection.setRequestProperty("X-Upload-Content-Length", );
urlConnection.setDoOutput(true);
String uploadFileName = "1541437905497.mp4";
String fileName = Environment.getExternalStorageDirectory().getPath() + "/cmxMediaGallery/1541437905497.mp4";
FileInputStream fileInputStream = new FileInputStream(fileName);
DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
dos.writeBytes(jsonObj.toString());
// this is not working
/*dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=" + uploadFileName + ";filename="
+ fileName + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
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.flush();
dos.close();
int responseCode = urlConnection.getResponseCode();
if (responseCode < 200 || responseCode >= 300) {
if ((responseCode + "").startsWith("4") && retry) {
return uploadMetaData(filePath, false, accessToken);
} else {
throw new IOException(String.format("response code='%s' (code %d)" + " for %s",
urlConnection.getResponseMessage(), responseCode, urlConnection.getURL()));
}
}
Log.d("Hong", "responseCode : " + responseCode);
return urlConnection.getHeaderField("Location");
}
某些示例使用多部分。但它正在工作。
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
我想知道如何在不使用youtube api的情况下通过JAVA中的HTTP POST上传视频文件。