【问题标题】:Getting 401 Youtube C# API with Google Secure AuthSub使用 Google Secure AuthSub 获取 401 Youtube C# API
【发布时间】:2012-02-07 07:43:51
【问题描述】:

我的问题是在从 Google SECURELY 获取会话令牌后发生的

这就是我所做的。 1) 获取一次令牌

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

2) 兑换会话 Token

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

非常感谢!

【问题讨论】:

    标签: c# youtube-api google-authentication


    【解决方案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 总是不安全的。如何在无需手动替换的情况下获取安全上传 url?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 2013-12-09
      • 2016-02-19
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      相关资源
      最近更新 更多