【问题标题】:YouTube API too_long error code on short keywords?短关键字上的 YouTube API too_long 错误代码?
【发布时间】:2012-03-17 03:11:10
【问题描述】:

我觉得在任何地方都找不到这个答案真的很愚蠢。有人可以指导我到某个地方或告诉我 YouTube 对关键字的限制是什么吗? 我发现限制曾经是 120 个字符,但后来 I heard it changed 在 2010 年 3 月增加到 500 个字符,所以如果这是真的,这对我来说应该不是问题。我手上可能还有另一个问题。也许有人可以帮我解决这个问题...

我在基于客户端的应用程序中使用YouTube API for Javadirect upload。我有大量视频要同时上传到多个不同的帐户(每个帐户都使用不同的语言)。所以我使用线程来完成这个。我将并发上传的数量限制为 10 只是为了安全起见。所有描述都不会超过 500 个字符,并且由于我遇到了这个错误,我让关键字自动限制为 120 个字符(我什至尝试将其限制为 70 个字符并且遇到了同样的问题)。所以,我完全不确定为什么会这样。我从谷歌得到的错误是:

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>

我也收到 InvalidEntryException 作为错误代码,但我稍后会处理(我认为这与我的身份验证超时或其他原因有关)。

无论如何,我不会在每个视频上都收到此错误。事实上,大多数视频都做得很好。我还没有测试找出哪些视频引发了错误(我会在完成这篇文章后这样做),但我无法理解为什么我会收到这个错误。我将在这里发布我的代码,但它几乎与api documentation 上的内容完全相同,所以我不知道它是否会有很大帮助。我猜我缺少一些基本的东西。

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;
  }

验证YouTube():

  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");
    }
  }

对此的任何帮助都会很棒!此外,在调用 uploadToYouTube() 方法之前,我打印出视频正在上传到 YouTube,在方法调用之后我打印出它已完成。有人可以解释为什么这些会在彼此之间瞬间打印出来吗?我没有为uploadToYouTube() 方法启动一个新线程,我猜想在youTubeService 的insert() 方法中为上传启动了一个新线程。不过这有点烦人,因为我不太确定视频在什么时候完成上传,如果我在程序完成之前停止程序,那么视频就会停止上传。

无论如何!感谢您阅读所有这些!希望有人能帮帮我!

【问题讨论】:

    标签: java client youtube-api gdata


    【解决方案1】:

    解决方案非常简单!问题是即使我没有超过总标签 500 个字符的限制,但有时我超过了每个标签 30 个字符的限制!我只是将标签添加到以下代码中:

    //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();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2014-04-02
      • 2013-04-07
      • 2012-09-28
      • 2022-01-19
      • 2019-06-17
      相关资源
      最近更新 更多