我對在C#中使用WebRequests很感興趣,並且想要幫助登錄到YouTube並將cookie存儲到cookie容器中。任何幫助將非常感激。使用C#中的WebRequests登錄到YouTube
4
A
回答
4
如果您使用的是.NET客戶端庫(http://code.google.com/p/google-gdata/),則需要時使用您提供的憑據進行身份驗證:
https://developers.google.com/youtube/2.0/developers_guide_dotnet#Authentication
的GData在C#
在使用的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
相關問題
- 1. C#登錄xenForo論壇使用Webrequests?
- 2. 使用c#登錄Youtube#
- 3. 使用C#異步WebRequests使用C#
- 4. Youtube使用OpenID登錄
- 5. 使用webrequests連接到asana
- 6. 使用C#登錄到Facebook頁面沒有登錄使用C#
- 7. Google數據登錄到YouTube?
- 8. C#汽WebRequests
- 9. 使用ASP.NET C#上傳到YouTube - 避免使用「Google登錄」屏幕
- 10. 如何使用C#的Youtube API登錄程序?
- 11. 使用Google登錄授權YouTube Data API
- 12. 如何使用Metro代碼登錄Youtube
- 13. 如何使用Perl登錄YouTube?
- 14. 如何使用PHP登錄Youtube?
- 15. 驗證登錄YouTube
- 16. 此C#代碼的說明登錄到youtube
- 17. 登錄使用Vimeo的C#
- 18. 如何使用C登錄到Craigslist#
- 19. 使用c自動登錄到網站#
- 20. 使用C#登錄到網頁
- 21. 使用Objective-C登錄/發佈到WordPress?
- 22. 使用C#登錄到Active Directory
- 23. 使用Angular登錄到WinApi C#
- 24. 登錄到YouTube會從安卓
- 25. webrequests c多線程程序#
- 26. Youtube Java API。使YouTubeService無需登錄
- 27. 使用C登錄網站#
- 28. 使用C#從xml登錄?
- 29. 使用GUI登錄Objective-C
- 30. Google+登錄Winform使用C#
謝謝您的回答。有沒有辦法使用API登錄並將詳細信息存儲到cookie容器中?稍後我會在代碼中需要cookie容器中的登錄數據。 – liamp47
查看我添加的代碼,這會給你如何使用它的相當好的想法。快樂編碼... –
謝謝,這正是我想要的。 – liamp47