Question

I'm implementing a service to upload videos to YouTube using their API (V2), but once the video is uploaded I need to find the response so I can get the destination URL. Anybody know how to achieve this?

Here's the call that calls the upload function:

YouTubeService service = new YouTubeService("My_Service", developerKey);

 try {
      service.setUserCredentials(user, password);
 } catch (AuthenticationException e) {
      System.out.println("Invalid login credentials.");
      return;
 }

 youtubeURL = YouTubeUploadClient.uploadVideo(service);

Here's the upload function code that as I said works:

public static String uploadVideo(YouTubeService service)
        throws IOException, ServiceException, InterruptedException, JSONException {

    String youtubeURL = null;
    File videoFile = new File("c://video.mp4");

    if (!videoFile.exists()) {
        System.out.println("Sorry, that video doesn't exist.");
        return null;
    }

    MediaFileSource ms = new MediaFileSource(videoFile, "video/mp4");
    String videoTitle = "Title";

    VideoEntry newEntry = new VideoEntry();
    YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
    mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Games"));
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent("Title");
    mg.setKeywords(new MediaKeywords());
    mg.getKeywords().addKeyword("Test");
    mg.setDescription(new MediaDescription());
    mg.getDescription().setPlainTextContent("Description");

    FileUploadProgressListener listener = new FileUploadProgressListener();
    ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(
            service, new URL("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"), ms, newEntry)
            .title(videoTitle)
            .trackProgress(listener, 1000)
            .chunkSize(10000000)
            .build();

    uploader.start();
    while (!uploader.isDone()) { Thread.sleep(25); }

    switch(uploader.getUploadState()) {
        case COMPLETE: System.out.println("Uploaded successfully"); return null;
        case CLIENT_ERROR: System.out.println("Upload Failed"); return null;
        default: System.out.println("Unexpected upload status"); return null;
    }
}

private static class FileUploadProgressListener implements ProgressListener {
    public synchronized void progressChanged(ResumableHttpFileUploader uploader)
    {
        switch(uploader.getUploadState()) {
            case COMPLETE: System.out.println("Upload Completed"); break;
            case CLIENT_ERROR: System.out.println("Upload Failed"); break;
            case IN_PROGRESS: System.out.println(String.format("Upload in progress: %3.0f", uploader.getProgress() * 100) + "%"); break;
            case NOT_STARTED: System.out.println("Upload Not Started"); break;
        }
    }
}
Was it helpful?

Solution

Found this response in the YouTube API forums (https://groups.google.com/forum/embed/?place=forum/youtube-api-gdata&showsearch=true&showpopout=true&parenturl=https%3A%2F%2Fdevelopers.google.com%2Fyoutube%2Fforum%2Fdiscussion#!searchin/youtube-api-gdata/resumable$20response/youtube-api-gdata/ox_c40kvZWQ/ADy-DWo16HkJ):

        ResumableGDataFileUploader uploader = new ResumableGDataFileUploader.Builder(
                service, new URL("http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads"), ms, newEntry)
                .title(videoTitle)
                .trackProgress(listener, 1000)
                .chunkSize(10000000)
                .build();

        Future<ResumableHttpFileUploader.ResponseMessage> futureResponse = uploader.start();

        while (!uploader.isDone()) { Thread.sleep(1000); }

        switch(uploader.getUploadState()) {
            case COMPLETE:
                ResumableHttpFileUploader.ResponseMessage responseMessage = futureResponse.get();
                VideoEntry videoEntry = new VideoEntry();
                videoEntry.parseAtom(new ExtensionProfile(), responseMessage.getInputStream());
                youtubeURL = videoEntry.getHtmlLink().getHref();
                logger.info("Uploaded successfully");
                return youtubeURL;
            case CLIENT_ERROR: logger.error("Upload Failed"); return null;
            default: logger.warn("Unexpected upload status"); return null;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top