JAVA Youtube µ¿¿µ»ó ¾÷·Îµå
2013/04/09 17:42
http://take0415.blog.me/60189703686
À¥ »çÀÌÆ® »ç¿ëÀÚµéÀÌ Æ¯Á¤ À¯ºäºê °èÁ¤À¸·Î µ¿¿µ»óÀ» ¿Ã¸®°Ô ÇÏ´Â ¹æ¹ý
(A¶ó´Â À¯ºäºê °èÁ¤À¸·Î a, b, c ¶õ »ç¶÷ÀÌ AÀÇ °èÁ¤À¸·Î µ¿¿µ»óÀ» ¿Ã¸®´Â ¹ý)
00. ±¸±Û °èÁ¤ »ý¼º
01. Developer Key »ý¼º
https://code.google.com/apis/youtube/dashboard/gwt/index.html#newProduct
Á¢¼Ó ÈÄ Á¤º¸ ÀÔ·Â ÈÄ save
02. API Project »ý¼º (Client ID »ý¼º)
https://code.google.com/apis/console/?pli=1
API Project (ÁÂÃø »ó´Ü ¼¿·ºÆ® ¹Ú½º ¸ð¾ç) > API Access ¼±Åà > Create an OAuth 2.0 client ID... ¹öÆ° Ŭ¸¯
Create Client ID ·¹À̾îÆ˾÷ÀÌ ¶å´Ï´Ù. (ÃÑ 2ÆäÀÌÁö)
- Branding Infomation : Á¤º¸ ÀÔ·Â
- Client ID Settings : Application typeÀº Web application ¼±Åà ³ª¸ÓÁö Á¤º¸ ÀÔ·Â
- Create ID ¹öÆ° Ŭ¸¯
¿Ï·á ÆäÀÌÁö¿¡¼ Create another client ID ¹öÆ°À» ÅëÇØ ÇâÈÄ Client ID Ãß°¡ »ý¼º °¡´É
03. Youtube ¼ºñ½º È°¼ºÈ
API Project (ÁÂÃø »ó´Ü ¼¿·ºÆ® ¹Ú½º ¸ð¾ç) > Services ¼±Åà > YouTube Data API v3À» ONÀ¸·Î º¯°æ (ÀÌ ¶§ µ¿ÀÇÇ϶ó´Â ÆäÀÌÁö°¡ ¶ß´Âµ¥ Agree ÇÏ½Ã¸é µÇ¿ä.)
04. Client Lib ´Ù¿î·Îµå
ÀÚ ÀÌÁ¦ JAR ÆÄÀϵ鸸 ´Ù¿î·Îµå ¹ÞÀ¸¸é Áغñ ÀÛ¾÷ÀÌ ¿Ï·á µÇ¿ä.
À¯ºäºê ÆÄÀÏ ¾÷·Îµå¿¡ ÇÊ¿äÇÑ JAR ÆÄÀÏ ¸ñ·ÏÀÔ´Ï´Ù.
½ÇÁ¦ Á¦°¡ ¼ºñ½º ½Ã ÇÊ¿äÇÑ ³ð¸¸ Ã߸° ³à¼®ÀÔ´Ï´Ù.
¹öÁ¯Àº Â÷ÀÌ°¡ ÀÖÀ» ¼ö Àְڳ׿ä.
ÀÏ´Ü ±¸±Û¿¡¼ Á¦°øÇÏ´Â ¶óÀ̺귯¸® + javax.mail À̳ðÀÌ ÇÊ¿äÇÕ´Ï´Ù. ¸ñ·ÏÀº ¾Æ·¡¿Í °°½À´Ï´Ù.
https://code.google.com/p/gdata-java-client/downloads/list
¿¡ °¡¼Å¼ gdata-src.java-1.47.1.zip ´Ù¿î·Îµå ÇϽŠÈÄ ¾Æ·¡ ¸ñ·ÏÀ¸·Î Ã߸®¸é µÇ´Âµ¥
À̶§ guava-11.0.2.jar, jsr305.jar ÆÄÀÏÀº deps Æú´õ ¾È¿¡ µé¾î ÀÖÀ¸´Ï Çì¸ÅÁö ¸¶¼¼¿ä. (Àü ¿Ã¸®´Ù Ŭ·¡½º ¾ø´Ù°í ¿¡·¯³ª¼ ÇÑÂü »ðÁúÇß½À´Ï´Ù...)
³ª¸ÓÁö´Â lib Æú´õ¿¡ µé¾î ÀÖ±¸¿ä.
- ±¸±Û¿¡¼ Á¦°ø Áß ÇÊ¿äÇÑ ¸ñ·Ï
/WEB-INF/lib/gdata-client-1.0.jar (¾Æ·¡·Î lib Æú´õ¿¡)
/WEB-INF/lib/gdata-client-meta-1.0.jar
/WEB-INF/lib/gdata-core-1.0.jar
/WEB-INF/lib/gdata-maps-2.0.jar
/WEB-INF/lib/gdata-maps-meta-2.0.jar
/WEB-INF/lib/gdata-media-1.0.jar
/WEB-INF/lib/gdata-youtube-2.0.jar
/WEB-INF/lib/gdata-youtube-meta-2.0.jar
/WEB-INF/lib/guava-11.0.2.jar (¾Æ·¡·Î deps Æú´õ¿¡)
/WEB-INF/lib/jsr305.jar
-- ±¸±Û°ú º°µµ·Î Áغñ
/WEB-INF/lib/mail-1.4.jar
¸ÞÀÌºì »ç¿ë ½Ã
<!-- javax.mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
·Î ´Ù¿î ¹ÞÀ¸½É µË´Ï´Ù.
À̷νá ÀÏ´Ü Áغñ´Â ¾óÃß ³¡³µ½À´Ï´Ù.
05 ÃÖÃÊ¿¡ Çѹø¸¸ ÇÊ¿äÇÑ ¼¼¼Ç ÅäÅ« ±¸Çϱâ
È帧¸¸ °£´ÜÈ÷ ¼³¸íµå¸®¸é
- ¤¡. ³»°¡ ¼ºñ½º ÇÒ À¥ ¾îÇø®ÄÉÀ̼ǿ¡¼ µÇµ¹¾Æ ¿Ã URLÀ» ÆĶó¹ÌÅÍ·Î Æ÷ÇÔÇÏ¿© À¯Æ©ºêÀÇ ÆäÀÌÁö·Î À̵¿ ½ÃÅ°°í
- ¤¤. À̵¿ µÈ À¯Æ©ºê ÆäÀÌÁö¿¡¼´Â µ¿¿µ»óÀÌ ¿Ã·ÁÁú °èÁ¤À¸·Î ±¸±Û ·Î±×ÀÎ ÈÄ ÀÎÁõ ¹öÆ° Ŭ¸¯ ½Ã ( ¤¡. ¹ø¿¡¼) º¸³½ ÆĶó¹ÌÅ͸¦ ±âº»À¸·Î
- ¤§. ´Ù½Ã ³ªÀÇ À¥ ¾îÇø®ÄÉÀ̼ÇÀ¸·Î µÇµ¹·Â Áִµ¥ À̶§ ÆĶó¹ÌÅÍ·Î Àӽà ÅäÅ«°ªÀ» ³Ñ±è
- ¤©. ±×·³ ³ª´Â Àӽ÷Π¹ÞÀº Àú ÅäÅ«°ªÀ» ¼¼¼Ç ÅäÅ«À¸·Î º¯°æÇØ ÀúÀåÇÏ°í ÀÖ´Ù°¡
- ¤±. À¯Æ©ºê ¼ºñ½º »ç¿ë ½Ã ÀúÀåÇØ µÐ ¼¼¼Ç ÅäÅ« °ªÀ» »ç¿ë
ÇÏ´Â ±¸Á¶Àε¥ ¤¡ ~¤© ±îÁö´Â ÃÖÃÊ¿¡ µü Çѹø¸¸ ÇØÁÖ¸é µÇ¿ä.
- ¤¡ »ó¼¼ ³»¿ë
¿¹¸¦ µé¾î ½ºÇÁ¸µÀ» ¾²½Å´ÙÇÏ¸é ¾Æ·¡Ã³·³ URL ¸¸µç ÈÄ YOUTUBE·Î Redirect ½Ãŵ´Ï´Ù.
±¸Áö ½ºÇÁ¸µÀ» ¾È ½áµµ response.sendRedirect("[BACK_URL]") ÇÏ¸é µÇÁÒ.
[BACK_URL] ÀÌ ¸ÕÁö´Â ¾Æ·¡ ¼³¸íÇß½À´Ï´Ù.
// @RequestMapping(value = "/youtube/oauth2call.do")
// public String oauth2call(ModelMap model) throws Exception {
// String requestUrl = AuthSubUtil.getRequestUrl([ BACK_URL ], "http://gdata.youtube.com", false, true);
// return "redirect:" + requestUrl;
// }
[ BACK_URL ] : CLIENT ID »ý¼ºÇÏ½Ç ¶§ https://code.google.com/apis/console/?pli=1 API Project (ÁÂÃø »ó´Ü ¼¿·ºÆ® ¹Ú½º ¸ð¾ç) > API Access ¼±Åà ÆäÀÌÁö º¸½Ã¸é »ý¼ºÇÑ Client ID ¾Æ·¡ ÂÊ¿¡ [ Redirect URIs ] À̶ó°í ³ª¿Â °ªÀ» ³Ö¾îÁÖ½Ã¸é µË´Ï´Ù.
- ¤¤ ÀÚ ÀÌÁ¦ ±¸±ÛÀÇ ÀÎÁõ ¿äû ÆäÀÌÁö°¡ ¶ß´Âµ¥ À̶§ ±¸±Û ·Î±×ÀÎÀÌ ¾È µÇ¾î ÀÖÀ¸¸é µ¿¿µ»óÀÌ ¿Ã·Á Áú °èÁ¤À¸·Î ·Î±×ÀÎ ÈÄ µ¿ÀÇ ¹öÆ° Ŭ¸¯Çϸé [¤¡] ¿¡¼ ³Ñ±ä BACK_URL·Î ´Ù½Ã À̵¿ µÊ
- ¤§/¤© ±¸±Û¿¡¼ ÀÓ½ÃÅäÅ«À» ÆĶó¹ÌÅÍ·Î ´ã¾Æ¼ ³ªÀÇ ÆäÀÌÁö·Î À̵¿ ½ÃÄÑ ÁáÀ¸´Ï ³ª´Â
// @RequestMapping(value = "/youtube/oauth2callback.do") <- [ BACK_URL À» /youtube/oauth2callback.do
ÀÌ°É·Î Çß¾î¿ä ] :
// public String oauth2callback(ModelMap model, HttpServletRequest req, HttpServletResponse res) throws Exception {
// String onetimeUseToken = AuthSubUtil.getTokenFromReply(req.getQueryString()); // ÀÓ½ÃÅäÅ«°ªÀ» °¡Á®¿È
// String sessionToken = AuthSubUtil.exchangeForSessionToken(onetimeUseToken, null); // ¼¼¼ÇÅäÅ«À¸·Î º¯°æ
// logger.info("sessionToken : " + sessionToken);
// return "redirect:/?" + sessionToken;
// }
sessionToken °ªÀ» ÀúÀåÇÕ´Ï´Ù. ÇÁ·ÎÆÛƼ ÆÄÀÏ·Î »©µç ¾îÂîÇÏµç ±â¾ïÇصӴϴÙ. ¾ÕÀ¸·Î ¸Å¹ø »ç¿ë ÇÒ ³à¼®ÀÔ´Ï´Ù.
- ¤± ÀÚ ÀÌÁ¦ ½ÇÁ¦ »ç¿ë ÇÏ´Â ¿¹½ÃÀÔ´Ï´Ù.
@RequestMapping(value = "/youtube/uploadFile.do")
public String uploadFile(ModelMap model) throws Exception {
try {
YouTubeService service = new YouTubeService(CLIENT_ID, DEVELOPER_KEY);
service.setAuthSubToken([À§¿¡¼ ÀúÁ¤ÇÑ ¼¼¼Ç ÅäÅ« °ª], null);
VideoEntry newEntry = new VideoEntry();
newEntry.setGeoCoordinates(new GeoRssWhere(37.0, -122.0));
newEntry.setLocation("MY_MY_MY_");
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Howto")); // ÁÖÀÇ "Howto" ¼öÁ¤ ½Ã Ä«Å×°í¸® °ªÀ» Á¤ÇØ Áø °ªÀÌ ÀÖÀ¸´Ï ¾Æ¹«°Å³ª ÀÔ·ÂÇÏ¸é ¿¡·¯³ª¿ä!
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "MY_MY_MY_"));
mg.setTitle(new MediaTitle());
mg.setPrivate(false);
mg.getTitle().setPlainTextContent("MY_MY_MY_");
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("MY_MY_MY_");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent("MY_MY_MY_");
URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
FormUploadToken token = service.getFormUploadToken(uploadUrl, newEntry);
model.addAttribute("token", token); // ÅäÅ« °ªÀ» ´ã¾Æ ¾Æ·¡ JSP¸¦ ¶Ù¾îÁà¿ä.
} catch (Exception e) {
e.printStackTrace();
}
return "cmmn/youtube/video_form";
}
------------------------------------------------------------------------------------------------------------------------
cmmn/youtube/video_form.jsp
<form name="frm" action="${token.url }?nexturl=[µÇµ¹¾Æ¿Ã URL]" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="hidden" name="token" value="${token.token }" />
<a href="#" id="go">µ¿¿µ»ó ¾÷·Îµå</a>
<p id="msg">¾÷·Îµå ÁßÀÔ´Ï´Ù. Àá½Ã¸¸ ±â´Ù·ÁÁÖ¼¼¿ä.</p>
</form>
------------------------------------------------------------------------------------------------------------------------
ÀÚ À̳ðÀÌ ¹¹³Ä ÇÏ¸é µ¿¿µ»ó ¾÷·Îµå ¿Ï·á°¡ µÇ¸é À¯ºäºê¿¡¼ ´Ù½Ã ¿ì¸® À¥ ¾îÇø®ÄÉÀ̼ÇÀÇ ÆäÀÌÁö¸¦ È£ÃâÇÏ´Â ÁÖ¼ÒÀÔ´Ï´Ù.
¸®ÅÏ µÉ ¶§ /youtube/uploadFileCallback.do?status=200&id=µ¿¿µ»óID
200 Àº ¼º°øÇß´Ù´Â ¸»À̱¸¿ä.
µ¿¿µ»óID´Â ¾Æ·¡Ã³·³ »ç¿ë°¡´ÉÇØ¿ä http://youtu.be/µ¿¿µ»óID °¡ µé¾î°¡¸é ¾÷·Îµå µÈ µ¿¿µ»ó ÆäÀÌÁö¸¦ È£Ãâ °¡´ÉÇØ¿ä
@RequestMapping(value = "/youtube/uploadFileCallback.do")
public String uploadFileResult(ModelMap model, HttpServletRequest req) throws Exception {
Param p = new Param(req); // °Á ÆĶó¹ÌÅÍ
model.addAttribute("status", p.get("status"));
model.addAttribute("id", p.get("id"));
return "cmmn/youtube/video_form_result";
}
ÀÚ ÀÌÁ¦ ½ÇÇàÇØ º¸¸é ÇϳªÀÇ ±¸±Û °èÁ¤À¸·Î ´©°¡ ¿Ã¸®µç Àú URL·Î ¿Ã¸®¸é ³ªÀÇ ±¸±Û °èÁ¤À¸·Î µ¿¿µ»óÀÌ ¾÷·Îµå µË´Ï´Ù...