使用 Google Secure AuthSub 获取 401 Youtube C# API

发布于 2025-01-03 17:42:05 字数 1680 浏览 4 评论 0原文

我的问题是在安全地从 Google 获取会话令牌后发生的,

这就是我所做的。 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() 位于 将 AuthSub 与 .NET 客户端库结合使用

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 中将 secure 设置为 null,而不是 getRsaKey()),则代码可以正常工作。有人可以告诉我我错过了什么吗?

非常感谢!

My problem occur after getting the session token from Google SECURELY

Here's what I have done.
1) Get one time token

string authSubUrl = AuthSubUtil.getRequestUrl("https://some.com/mypage.aspx", "http://gdata.youtube.com", true, true);
Response.Redirect(authSubUrl);

2) Exchange for session Token

Session["YT_Token"] = AuthSubUtil.exchangeForSessionToken(Request.QueryString["token"], getRsaKey());

The getRsaKey() is found at Using AuthSub with the .NET Client Library

3) Make YouTube request

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);

This is where I get the error. At ytRequest.CreateFormUploadToken. I get this

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

The code works fine if I am NOT using a secure AuthSub (i.e. setting secure to false in step 1 and null instead of getRsaKey() in step 2). Can somebody tell me what I am missing?

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-01-10 17:42:05

已找到解决方案,但仍有一个小问题。
解决方案位于 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 始终不安全。如何获取安全上传网址而无需手动替换?

Solution found but there's still one small problem.
Solution found at http://code.google.com/p/google-gdata/issues/detail?id=393 , it's not that obvious.

Step 3 should look like this
3) Make YouTube request

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";

Notice the last line. formToken.Url is always insecure. How can I get the secure upload url without having to replace it manually?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文