如何使用OAuth2访问令牌(.NET)通过API上传Youtube视频?

时间:2013-01-06 00:37:15

标签: c# .net api upload youtube

我是一名经验丰富的.NET开发人员,我正在制作一个视频上传应用程序,只需要使用2个或3个帐户。使用1个帐户作为我的测试用例我已经使用它授权了我的应用程序,获得了我的授权密钥,现在我有了访问+刷新令牌。

进入这个阶段我现在意识到我不知道如何使用我的访问令牌上传视频(我知道我可能需要在回答时刷新它) - 有人可以帮忙吗?我找不到任何关于使用Youtube .NET客户端库(例如YouTubeRequestSettings,YouTubeRequest等)来执行此操作的文档 - 所有这些都非常有用!

1 个答案:

答案 0 :(得分:2)

我最终通过反复试验证明了这一点 - 脱节的Youtube API文档确实无济于事!

AuthSub已被弃用,如果您经历了获取OAuth2密钥的麻烦(文档中对此进行了详细描述),您可以使用它上传视频。

为此,只需使用access_token代替'YoutubeRequestSettings'对象中的authsub键 - 如下例所示:

string myDeveloperKey = "Your developer key here";
string myOAuthKey = "The OAuth key for the target user's account here";

YouTubeRequestSettings settings = new YouTubeRequestSettings("My Uploader App Name", myDeveloperKey, myOAuthKey); // The documentation for this method implies it only accepts an authSub key - it works if you pass your OAuth key

YouTubeRequest request = new YouTubeRequest(settings);

Video newVideo = new Video();

newVideo.Title = "Test Video - ignore me";
newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "test 1 , test 2";

newVideo.Description = "test 3 test 4";
newVideo.YouTubeEntry.Private = false;

newVideo.Tags.Add(new MediaCategory("tag 1, tag 2", YouTubeNameTable.DeveloperTagSchema));
newVideo.Private = true; // Make this video private as it's a test

newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);

newVideo.YouTubeEntry.MediaSource = new MediaFileSource(@"C:\MyTestVideo.mov", "video/quicktime");

Video createdVideo = request.Upload(newVideo);          
相关问题