kentcdodds
kentcdodds

Reputation: 29021

YouTube API too_long error code on short keywords?

I feel really silly to not be able to find this answer anywhere. Could someone please direct me to a place or inform me of what the YouTube limits are with regard to keywords? I found that the limit was once 120 characters, but then I heard it changed in March 2010 to 500 characters so it shouldn't be a problem for me if that's true. It's possible I have another problem on my hands. Maybe someone could help me with that one...

I'm using the YouTube API for Java using direct upload from a client based application. I have tons of videos I'm trying to upload to several different accounts at once (each in a different language). So I'm using threads to accomplish this. I limit the number of concurrent uploads to 10 just to try and play it safe. None of the descriptions will be much longer than 500 characters and because of this error I'm getting, I had the keywords automatically limit themselves to 120 characters (I even tried to limit it to 70 characters and had the same problem). So, I'm not sure at all why this is happening. The error I get from Google is:

SEVERE: null
com.google.gdata.util.InvalidEntryException: Bad Request
<?xml version='1.0' encoding='UTF-8'?>
  <errors>
    <error>
      <domain>yt:validation</domain>
      <code>too_long</code>
      <location type='xpath'>media:group/media:keywords/text()</location>
    </error>
  </errors>

I'm also getting an InvalidEntryException as an error code, but I'll deal with that later (I think it has to do with my authentication timing out or something).

Anyway, I don't get this error on every video. In fact, most videos make it just fine. I haven't yet tested to find out which videos are throwing the error (I will do that when I'm finished with this post), but it's beyond me why I'm getting this error. I'll post my code here, but it's pretty much exactly what's on the api documentation, so I don't know whether it'll be much help. I'm guessing there's something fundamental I'm missing.

uploadToYouTube():

  private void uploadToYouTube() throws IOException, ServiceException {
    TalkContent talkContent = transfer.getTalkContent();
    YouTubeService youTubeService = talkContent.getLanguage().getYouTubeService(); //see code for this below...

    String title = talkContent.getTitle();
    String category = talkContent.getCategory();
    String description = talkContent.getDescription();
    List<String> tags = talkContent.getTags();
    boolean privateVid = true;

    VideoEntry newEntry = new VideoEntry();
    YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();

    //Title
    mg.setTitle(new MediaTitle());
    mg.getTitle().setPlainTextContent(title);

    //Description
    mg.setDescription(new MediaDescription());
    mg.getDescription().setPlainTextContent(description);

    //Categories and developer tags
    mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, category));
    mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "kentcdodds"));
    mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "moregoodfoundation"));

    //Keywords
    mg.setKeywords(new MediaKeywords());
    int tagLimit = 70; // characters
    int totalTags = 0; //characters
    for (String tag : tags) {
      if ((totalTags + tag.length()) < tagLimit) {
        mg.getKeywords().addKeyword(tag);
        totalTags += tag.length();
      }
    }

    //Visible status
    mg.setPrivate(privateVid);

    //GEO coordinantes
    newEntry.setGeoCoordinates(new GeoRssWhere(40.772555, -111.892480));

    MediaFileSource ms = new MediaFileSource(new File(transfer.getOutputFile()), transfer.getYouTubeFileType());
    newEntry.setMediaSource(ms);

    String uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";

    VideoEntry createdEntry = youTubeService.insert(new URL(uploadUrl), newEntry);
    status = Transfer.FINISHEDUP;
  }

getYouTubeService():

  public YouTubeService getYouTubeService() {
    if (youTubeService == null) {
      try {
        authenticateYouTube();
      } catch (AuthenticationException ex) {
        JOptionPane.showMessageDialog(null, "There was an authentication error!" + StaticClass.newline + ex, "Authentication Error", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Language.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    return youTubeService;
  }

authenticateYouTube():

  public void authenticateYouTube() throws AuthenticationException {
    if (youTubeService == null) {
      System.out.println("Authenticating YouTube Service");
      youTubeService = new YouTubeService("THENAMEOFMYPROGRAM", "THEDEVELOPERIDHERE");
      youTubeService.setUserCredentials(youTubeUsername, youTubePassword);
      System.out.println("Authentication of YouTube Service succeeded");
    }
  }

Any help on this would be great! Also, before I call the uploadToYouTube() method, I print out that the video's being uploaded to YouTube and after the method call I print out that it finished. Can someone explain why those are printed out within moments of one another? I'm not starting a new thread for the uploadToYouTube() method, I'm guessing that in the insert() method on the youTubeService there's a new thread started for the upload. It's a little annoying though because I'm never quite sure at what point the video finishes uploading and if I stop the program before it's through then the video stops uploading.

Anyway! Thanks for reading all of this! I hope someone can help me out!

Upvotes: 2

Views: 1105

Answers (1)

kentcdodds
kentcdodds

Reputation: 29021

The solution was really simple! The problem was even though I'm not over the 500 character limit for total tags, but sometimes I was over the limit of 30 characters per tag! I just changed tag adding lines to the following code:

//Keywords
mg.setKeywords(new MediaKeywords());
int totalTagsLimit = 500; // characters
int singleTagLimit = 30; // characters
int totalTags = 0; //characters
for (String tag : tags) {
  if ((totalTags + tag.length()) < totalTagsLimit && tag.length() < singleTagLimit) {
    mg.getKeywords().addKeyword(tag);
    totalTags += tag.length();
  }
}

Upvotes: 3

Related Questions