从Google SECURELY
获取会话令牌后,我的问题就出现了这就是我所做的。 1)获得一次性令牌
string authSubUrl = AuthSubUtil.getRequestUrl("https://some.com/mypage.aspx", "http://gdata.youtube.com", true, true);
Response.Redirect(authSubUrl);
2)交换会话令牌
Session["YT_Token"] = AuthSubUtil.exchangeForSessionToken(Request.QueryString["token"], getRsaKey());
getRsaKey()位于Using AuthSub with the .NET Client Library
3)提出YouTube请求
YouTubeRequestSettings settings = new YouTubeRequestSettings("my app name", "Google.Client.ID", "Google.Youtube.DeveloperKey", (string)Session["YT_Token"]);
YouTubeRequest ytRequest = new YouTubeRequest(settings);
...
Video newVideo = new Video();
newVideo.Title = "blah";
newVideo.Tags.Add(new MediaCategory("People", YouTubeNameTable.CategorySchema));
newVideo.Description = "des";
newVideo.YouTubeEntry.Private = true;
FormUploadToken formToken = ytRequest.CreateFormUploadToken(newVideo);
这是我收到错误的地方。在ytRequest.CreateFormUploadToken。我明白了
Execution of request failed: http://gdata.youtube.com/action/GetUploadToken Google.GData.Client.GDataRequestException: Execution of request failed: http://gdata.youtube.com/action/GetUploadToken ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse()
ResponseString is yt:authenticationUnknown
如果我不使用安全的AuthSub(即在步骤1中将secure设置为false而在步骤2中设置为null而不是getRsaKey()),则代码可以正常工作。有人能告诉我我错过了什么吗?
非常感谢!
答案 0 :(得分:1)
找到解决方案,但仍然存在一个小问题。 在http://code.google.com/p/google-gdata/issues/detail?id=393找到的解决方案,并不是那么明显。
第3步应该是这样的 3)提出YouTube请求
YouTubeRequestSettings settings = new YouTubeRequestSettings("my app name", "Google.Client.ID", "Google.Youtube.DeveloperKey", (string)Session["YT_Token"]);
YouTubeRequest ytRequest = new YouTubeRequest(settings);
ytRequest.Service.RequestFactory =
new GAuthSubRequestFactory("youtube", "my app name")
{ PrivateKey = getRsaKey(), Token = (string)Session["YT_Token"] };
...
Video newVideo = new Video();
...
FormUploadToken formToken = ytRequest.CreateFormUploadToken(newVideo);
Form.Action = formToken.Url.Replace("http://", "https://") + "?nexturl=some page";
注意最后一行。 formToken.Url总是不安全的。如何在不必手动更换安全上传网址的情况下获取安全上传网址?