유투브 관련 안드로이드 어플 만드는 중입니다.
https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol 이 사이트 참고해서 업로드 기능을 넣는 중입니당.
(업로드 기능이 꼭 필요하다기 보단....걍 공부하는 셈 치고 넣는 중입니당)
업로드할 동영상의 메타데이터를 POST 요청으로 보내려고 하는데요
그래서 만든 메소드가....
private String uploadMetaData(String filePath, boolean retry) throws IOException { String title = getTitleText(); String description = getDescriptionText(); String category = DEFAULT_VIDEO_CATEGORY; String tags = DEFAULT_VIDEO_TAGS; if (getTagsText() != null && getTagsText().trim().length() != 0) { tags = getTagsText(); } String urlData = "uploadType=resumable&part=snippet,status,contentDetails"; String uploadUrl = INITIAL_UPLOAD_URL + "?" + urlData; 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", ); // urlConnection.setRequestProperty("X-Upload-Content-Length", ); urlConnection.setDoOutput(true); 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, title, description, tags); DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); dos.writeBytes(postData); dos.flush(); dos.close(); int responseCode = urlConnection.getResponseCode(); if (responseCode < 200 || responseCode >= 300) { // The response code is 40X if ((responseCode + "").startsWith("4") && retry) { Log.d(LOG_TAG, "retrying to fetch auth token for " + name); // this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken); // Try again with fresh token return uploadMetaData(filePath, false); } else { throw new IOException(String.format("response code='%s' (code %d)" + " for %s", urlConnection.getResponseMessage(), responseCode, urlConnection.getURL())); } } Log.d("크르릉", "urlConnection.getHeaderField " + urlConnection.getHeaderField("Location")); return urlConnection.getHeaderField("Location"); }
인데요....
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, title, description, tags); DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); dos.writeBytes(postData); dos.flush(); dos.close();
이 부분이 없으면 일단 업로드는 되요. 다만 title, description, tags 이런 정보가 업로드 되지 않고 동영상만 업로드 되네요...
(동영상 업로드는 위의 메소드에서 받은 리턴값을 이용해서 다른 메소드에서 처리합니당)
암만 생각해봐도 postDataForm 부분이 잘못된 거 같은데요..... 어케 해야 하는걸까요 ㅜㅜ
어제 저녁부터 새벽까지 저거 하나땜에 고생이네요.... 요리 바꾸고 조리 바꾸고 해봤는데 계속 실패 ㅜㅜ
도움이 필요합니다~