4

私は C# で WebRequests を使用するのにかなり慣れていないので、YouTube にログインして Cookie コンテナーに Cookie を保存する方法を教えてください。どんな助けでも大歓迎です。

4

1 に答える 1

5

.NET クライアント ライブラリ (http://code.google.com/p/google-gdata/) を使用している場合は、提供された資格情報を使用して、必要に応じて認証が行われます。

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

C# の GData

GData API を使用する場合、明示的なログアウト方法はありません。トークンを無効にすることはできますが、トークンを使用しないと、しばらくすると期限切れになります。トークンを無効にする方法の具体的な詳細は、採用されている認証メカニズム (OAuth、AuthSub、ClientLogin) によって異なります。

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

于 2012-10-09T09:52:15.600 に答える