如何在YouTube-API Java中正确应用yt:accessControl

时间:2012-03-14 14:27:10

标签: java xml permissions youtube-api

我在YouTube上有一个我上传的视频,我想改变它:accessControls on(特别是评论调节)。我查看过YouTube API参考herehere,但我似乎无法正确更新。这是我的代码:

//Strings declared beforehand: appName, developerId, username, password, and vidId.
YouTubeService service = new YouTubeService(appName, developerId);
service.setUserCredentials(username, password);

String videoEntryUrl = "http://gdata.youtube.com/feeds/api/users/default/uploads/" + vidId;
VideoEntry createdEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);

String originalXMLBlob = createdEntry.getXmlBlob().getBlob();

String atomXml = "<?xml version='1.0'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005' gd:fields='yt:accessControl' xmlns:yt='http://gdata.youtube.com/schemas/2007'><yt:accessControl action='comment' permission='moderated'/></entry>";

GDataRequest request = service.createPatchRequest(new URL(createdEntry.getEditLink().getHref()));
request.getRequestStream().write(atomXml.getBytes("UTF-8"));
request.execute();
createdEntry = service.parseResponseData(request, VideoEntry.class);

String newXMLBlob = createdEntry.getXmlBlob().getBlob().substring(49);
//The substring(49) is because after you do a service.parseResponseData... it attaches a timestamp to the xmlBlob. To properly compare whether these are the same in the printlns I substring it.

System.out.println(newXMLBlob.equals(originalXMLBlob));
System.out.println(originalXMLBlob);
System.out.println(newXMLBlob);
createdEntry.update();
createdEntry.update();

newXMLBlob = createdEntry.getXmlBlob().getBlob().substring(49);
System.out.println(newXMLBlob.equals(originalXMLBlob));
System.out.println(originalXMLBlob);
System.out.println(newXMLBlob);
System.out.println("Done");

输出结果为:

false
<yt:accessControl permission='allowed' action='comment'/><yt:accessControl permission='allowed' action='commentVote'/><yt:accessControl permission='moderated' action='videoRespond'/><yt:accessControl permission='allowed' action='rate'/><yt:accessControl permission='allowed' action='embed'/><yt:accessControl permission='allowed' action='list'/><yt:accessControl permission='allowed' action='autoPlay'/><yt:accessControl permission='allowed' action='syndicate'/>
<yt:accessControl permission='moderated' action='comment'/><yt:accessControl permission='allowed' action='commentVote'/><yt:accessControl permission='moderated' action='videoRespond'/><yt:accessControl permission='allowed' action='rate'/><yt:accessControl permission='allowed' action='embed'/><yt:accessControl permission='allowed' action='list'/><yt:accessControl permission='allowed' action='autoPlay'/><yt:accessControl permission='allowed' action='syndicate'/>
false
<yt:accessControl permission='allowed' action='comment'/><yt:accessControl permission='allowed' action='commentVote'/><yt:accessControl permission='moderated' action='videoRespond'/><yt:accessControl permission='allowed' action='rate'/><yt:accessControl permission='allowed' action='embed'/><yt:accessControl permission='allowed' action='list'/><yt:accessControl permission='allowed' action='autoPlay'/><yt:accessControl permission='allowed' action='syndicate'/>
<yt:accessControl permission='moderated' action='comment'/><yt:accessControl permission='allowed' action='commentVote'/><yt:accessControl permission='moderated' action='videoRespond'/><yt:accessControl permission='allowed' action='rate'/><yt:accessControl permission='allowed' action='embed'/><yt:accessControl permission='allowed' action='list'/><yt:accessControl permission='allowed' action='autoPlay'/><yt:accessControl permission='allowed' action='syndicate'/>
Done

重要的是,从始至终没有任何改变。您可以看到我甚至尝试过createdEntry.update();,但这也不起作用。任何帮助将不胜感激!谢谢!

哦,还有最后一件事。我希望我的atomXml能够将访问控制设置为多个事物(视频响应,评级等)。我猜你只是添加了更多的yt:accessControl标签,但在回答时,如果你有任何提示,我会很感激。再次感谢。

编辑:删除了xml中url中的空格。这导致了输出的变化(也更新了),这更有意义,但它不是理想的解决方案。

1 个答案:

答案 0 :(得分:2)

如果我最初没有在xml中解决空间问题,我就不必发布这个问题了。事实证明,您无法在上传视频后立即更新yt:accessControl。所以相反,我正在保存Ids和我上传它们,然后当它们全部完成时,我将更新yt:accessControl上的每一个。希望这有助于后来者。

哦,顺便说一句,我很确定你可以一次提交几个yt:accessControls。快乐的编码!