【发布时间】:2014-07-02 17:16:06
【问题描述】:
因为 Youtube Data Api v3 还不允许为视频禁用 cmets...我正在尝试使用 Youtube Data API v2 检索视频,更新其设置以禁用 cmets 并将其上传回来。
几天前我能够做到这一点,但我不确定为什么现在无法正常工作。我遇到的一件事是当我检索视频时,我可以更新设置,当我将其上传回来时,我得到一个空引用异常。奇怪。
看起来视频的 responseURI 作为 system.SystemNullReference 异常返回。
这是我正在使用的代码:
private void DisableComments(string videoId)
{
Google.YouTube.Video ourVideoToUpdate = SetAcessControl( channelId, videoId, "comment", "denied");
UpdateVideoSettings( ourVideoToUpdate);
}
private Google.YouTube.Video SetAcessControl(string channelId, string videoId, string type, string permission)
{
Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/users/" + channelId + "/uploads/" + videoId);
Google.YouTube.YouTubeRequestSettings settings = new Google.YouTube.YouTubeRequestSettings("VideoGenerator", DeveloperKeyHere, UsernameHere, PasswordHere);
Google.YouTube.YouTubeRequest ourRequest = new Google.YouTube.YouTubeRequest(settings);
Google.YouTube.Video videoToAdjust = new Google.YouTube.Video();
try
{
Google.YouTube.Video video = ourRequest.Retrieve<Google.YouTube.Video>(videoEntryUrl);
var exts = videoToAdjust.YouTubeEntry.ExtensionElements
.Where(x => x is Google.GData.Extensions.XmlExtension)
.Select(x => x as Google.GData.Extensions.XmlExtension)
.Where(x => x.Node.Attributes != null && x.Node.Attributes["action"] != null && x.Node.Attributes["action"].InnerText == type);
var ext = exts.FirstOrDefault();
if (ext != null) ext.Node.Attributes["permission"].InnerText = permission;
}
catch (Google.GData.Client.GDataRequestException ex)
{
videoToAdjust = null;
}
return videoToAdjust;
}
private void UpdateVideoSettings(Google.YouTube.Video videoToUpdate)
{
Google.YouTube.YouTubeRequestSettings settings = new Google.YouTube.YouTubeRequestSettings("VideoGenerator", DeveloperKeyHere, UsernameHere, passwordHere);
Google.YouTube.YouTubeRequest ourUpdateRequest = new Google.YouTube.YouTubeRequest(settings);
try
{
// NULL REFERENCE EXCEPTION HERE - cant upload
ourUpdateRequest.Update<Google.YouTube.Video>(videoToUpdate);
}
catch (Exception ex)
{
}
}
如果能帮助您了解如何更新此视频,我们将不胜感激 - 谢谢!
【问题讨论】:
标签: c# youtube-api youtube-data-api