使用Youtube .net API上传视频并将其设置为不公开

时间:2011-02-08 10:59:35

标签: c# .net youtube

似乎.net的Youtube API暂时没有更新。因此,没有任何属性或方法可以将视频设置为未列出。如果他们之前遇到过这个问题,有人可以提出建议吗?

3 个答案:

答案 0 :(得分:7)

我也很难解决这个问题,所以我想我会发现我的发现给任何寻找答案的人。

根据this thread,在rev中添加了对yt:accessControl的支持。 1118。

在撰写本文时,该修订版不包含在您从Google的API下载页面下载的API中。您必须获取最新版本的API here(SVN Checkout)。

一旦你有了这个,你可以做这样的事情:

Video newVideo = new Video();
newVideo.YouTubeEntry.AccessControls.Add(new YtAccessControl("list", "denied"));

干杯!

答案 1 :(得分:2)

这篇文章对我有很大的帮助:

How do I disable comments and ratings using the YouTube API asp.net

我最终必须修改代码以为属性列表添加空检查:

private Video SetAcessControl(Video video, string type, string permission)
    {
        var exts = video.YouTubeEntry.ExtensionElements
                        .Where(x => x is XmlExtension)
                        .Select(x => x as 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;

        return video;
    }

然后,使用它:

        YouTubeRequest request = CreateYouTubeRequest(configuration);
        Uri youTubeUrl = new Uri(string.Format("http://gdata.youtube.com/feeds/api/users/default/uploads/{0}", youTubeVideoId));
        Video video = request.Retrieve<Video>(youTubeUrl);

        video = SetAcessControl(video, "list", "denied");  // removes the video from searches, thus making it Unlisted (what you're looking for)
        video = SetAcessControl(video, "comment", "denied");  // disables comments
        video = SetAcessControl(video, "commentVote", "denied"); // disables voting on comments
        video = SetAcessControl(video, "videoRespond", "denied"); // disables video responses
        video = SetAcessControl(video, "rate", "denied"); // disables rating

        Video updatedVideo = request.Update(video);

非常重要的是要注意,这不能应用于您正在上传的视频(即,在调用request.Upload(视频)之前,您无法将其应用于新的Video()。您需要等到之后在此代码生效之前,上传过程已完成。

要查看可以使用此方法禁用的项目的完整列表,请参阅以下网址: http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_yt:accessControl

希望这有帮助!

答案 2 :(得分:2)

使用“YouTubeRequestSettings”传递用户名和密码。

实施例

YouTubeRequestSettings settings = new YouTubeRequestSettings("My Channel", YouTubeDeveloperKey, "username", "password");

如果您想要检索“不公开”或“私人”视频,则需要根据您的请求传递身份验证。