Google YouTube API Uploading Videos From Your Web Application To YouTube Using Java
Posted by tech on
March 26, 2008
Google YouTube API lets users who want their web applications to be able to upload videos to the YouTube site as well as displaying the user’s uploaded videos. First things first. As the developer, you need to get a developer’s key. You need this to be able to perform write or upload video operations. Go over to this link. Once you have finished submitting your developer information, you need to register a product in order to get a developer key. Once you’ve finished registering, you actually also get a ClientID aside from the developer key. You need both of these two in your code.
To start with, we need to initialize a YouTubeService object which we use for account validation.
public YouTubeService service; public void init() { if (service == null) { service = new YouTubeService(YOUTUBE_CLIENTID, YOUTUBE_DEVELOPERKEY); String username = YOUTUBE_USERNAME; String password = YOUTUBE_PASSWORD; try { service.setUserCredentials(username, password); } catch (AuthenticationException ae) { ae.printStackTrace(); } } }
Then we call this method
String token, formUrl; public static void setFormDetails() throws IOException { VideoEntry newEntry = new VideoEntry(); YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup(); String videoTitle = "this is a title"; mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos")); mg.setTitle(new MediaTitle()); mg.setPrivate(false); mg.setKeywords(new MediaKeywords()); mg.getKeywords().addKeyword(""); mg.getTitle().setPlainTextContent(videoTitle); mg.setDescription(new MediaDescription()); mg.getDescription().setPlainTextContent(videoTitle); URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken"); try { FormUploadToken fut = service.getFormUploadToken(uploadUrl, newEntry); token = fut.getToken(); formUrl = fut.getUrl(); } catch (ServiceException se) { se.printStackTrace(); } }
The setFormDetails() method sets two variables token and formUrl which are required values in your HTML form fields. The formUrl would serve as your action URL of the form tag while the token variable will be a hidden field. The action URL must also append a nexturl parameter in order for your browser to redirect it to that URL after upload is finished. Your HTML form field would look like this:
<form action="FORM_URL?nexturl=http://mydomain.com" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="hidden" name="token" value="TOKEN_VALUE"/> <input type="submit" value="go" /> </form>
Within the setFormDetails() method, the YouTubeNamespace.CATEGORY_SCHEME lets you specify which Category the video will be set as. This method is purely manual. You can create your own form page with title, description and category that lets the user sets it according to his preference. Just make sure those values are not empty because as they are required by the YouTubeAPI.
Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.





