2012-02-27 33 views
2

我覺得真的很無聊,無法在任何地方找到答案。有人可以指示我到某個地方嗎?或者告訴我關於關鍵字的YouTube限制是什麼? 我發現限制是一次120個字符,但在2010年3月I heard it changed 500個字符,所以它不應該是一個問題,如果這是真的。我可能有另一個問題在我手上。也許有人可以幫助我用那個...YouTube API too_long短關鍵字的錯誤代碼?

我使用YouTube API for Java使用direct 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; 
    } 

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

任何幫助將是巨大的!此外,在我致電uploadToYouTube()方法之前,我打印出該視頻正在上傳到YouTube,並在方法調用後打印出它已完成。有人可以解釋爲什麼這些打印出來彼此的時刻?我沒有爲uploadToYouTube()方法啓動一個新線程,我猜測在youTubeService的insert()方法中,有一個新的線程開始上傳。這有點煩人,因爲我從來不確定視頻完成上傳的時間點,如果在播放完之前停止該節目,視頻將停止上傳。

無論如何!感謝您閱讀所有這些!我希望有人能幫助我!

回答

3

解決方案非常簡單!問題是即使我沒有超過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(); 
    } 
}