YouTube API身份验证问题

时间:2010-02-19 14:11:35

标签: c# .net youtube-api

我正在尝试使用YouTube API从浏览器上传视频。我阅读了Google API文档并编写了这段代码:

 public static YouTubeRequest GetRequest()
    {
        var request = HttpContext.Current.Session["YTRequest"] as YouTubeRequest;
        if (request == null)
        {
            var settings = new YouTubeRequestSettings("WebApp", "NA", ConfigurationManager.AppSettings["YouTubeAPIDeveloperKey"]);

            settings.AutoPaging = true;
            request = new YouTubeRequest(settings);
            HttpContext.Current.Session["YTRequest"] = request;
        }
        return request;
    }

var youTubeRequest = GetRequest();
var newVideo = new Video { Title = "Title", Description = "Description" };
newVideo.YouTubeEntry.Private = false;

var token = youTubeRequest.CreateFormUploadToken(newVideo);
var postUrl = token.Url;
var tokenValue = token.Token;

但我总是收到此错误消息: 远程服务器返回错误:(401)未经授权。

在这一行:

var token = youTubeRequest.CreateFormUploadToken(newVideo);

我认为我的问题与Developer API密钥无关。它是从API Dashboard正确抓取的。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我自己发现了这个问题!

newVideo.Keywords = "some keywords";

是必需的,你应该发送它。

请注意,我之前解释过我更改了YouTubeRequest参数以消除401错误消息(请参阅我自己的评论)。