c# - 在 C# 中使用 WebRequests 登录 YouTube

标签 c# authentication youtube

我对在 C# 中使用 WebRequests 还很陌生,希望获得一些帮助来登录 YouTube 并将 cookie 存储到 cookie 容器中。任何帮助将不胜感激。

最佳答案

如果您使用的是 .NET 客户端库 (http://code.google.com/p/google-gdata/),那么它将在需要时使用您提供的凭据为您进行身份验证:

https://developers.google.com/youtube/2.0/developers_guide_dotnet#Authentication

C# 中的 GData

当使用 GData API 时,没有明确的注销方法,您可以使您的 token 无效,但如果您不使用它,它也会在一段时间后过期。根据所采用的身份验证机制(OAuth、AuthSub、ClientLogin),有关如何使 token 失效的具体细节有所不同。

你也可以引用CodeProject上的这篇文章,Manage YouTube using C# and Youtube API 1.6

YouTubeService service = new YouTubeService();
service.setUserCredentials(txtUser.Text , txtPassword.Text);
try { service.QueryClientLoginToken(); }
catch(System.Net.WebException e) { MessageBox.Show(e.Message); }

已添加:我已调整以下代码以满足您的要求。

class YouTube
{
    public void Login()
    {
        HttpWebRequest request = GetNewRequest("https://accounts.google.com/ServiceLoginAuth", cookies);
        request.Referer = "https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F&uilel=3&hl=en_US&service=youtube";
        request.Host = "accounts.google.com";
        Dictionary<string, string> parameters = new Dictionary<string, string>{
            {"continue","https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F"},
            {"service","youtube"},{"uilel","3"},{"dsh","157212168103955870"},{"hl","en_US"},
            {"GALX","PTqcwpZb2aE"},{"pstMsg","1"},{"dnConn",""}, {"checkConnection","youtube%3A248%3A1"}, 
            {"checkedDomains","youtube"}, {"timeStmp",""}, {"secTok",""}, {"Email","username"}, {"Passwd","password"}, 
            {"signIn","Sign+in"}, {"PersistentCookie","yes"}, {"rmShown","1"}};
        HttpWebResponse response = MakeRequest(request, cookies, parameters);
        response.Close();
    }

    private static CookieContainer cookies = new CookieContainer();

    private static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
        request.CookieContainer = SessionCookieContainer;
        request.AllowAutoRedirect = false;
        return request;
    }

    private static HttpWebResponse MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
    {
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.CookieContainer = SessionCookieContainer;
        request.AllowAutoRedirect = false;

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        string postData = string.Empty;
        foreach (KeyValuePair<String, String> parametro in parameters)
        {
            if (postData.Length == 0)                
                postData += String.Format("{0}={1}", parametro.Key, parametro.Value);                
            else                
                postData += String.Format("&{0}={1}", parametro.Key, parametro.Value);                
        }

        byte[] postBuffer = UTF8Encoding.UTF8.GetBytes(postData);
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(postBuffer, 0, postBuffer.Length);
        }

        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        SessionCookieContainer.Add(response.Cookies);

        while (response.StatusCode == HttpStatusCode.Found)
        {
            response.Close();
            request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
            response = (HttpWebResponse)request.GetResponse();
            SessionCookieContainer.Add(response.Cookies);
        }
        return response;
    }
}

引用:http://social.msdn.microsoft.com/Forums/pl-PL/ncl/thread/40d249b5-a9ad-4068-8853-629fb20584a0

关于c# - 在 C# 中使用 WebRequests 登录 YouTube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12797238/

相关文章:

c# - 如何创建公共(public)操作,但保持调用私有(private)?

c# - 如何从 HTML 提交按钮调用 ASP.NET CORE WEB API 3.1

c# - SqlDatareader GetValue 根据系统日期格式返回日期值

asp.net-core - Saml 无 Cookie 保留状态 ASP.NET CORE

wcf - 安全的 WCF 服务,除了 SSL 协议(protocol)之外还需要什么样的身份验证?

c# - 如何使用.net youtube API直接上传获取上传状态

javascript - iframe 中嵌入的页面中的 YouTube 视频在 Firefox 中不起作用

c# - 一次播放多个声音文件

ruby-on-rails - 设计路由错误

php - youtube api json php