public static class GoogleAuthenticationApi
{
public static string GetAuthenticationToken(string userName, string password, out bool isUserValid)
{
string response = GoogleRequestHelper.GetPostResult("https://www.google.com/accounts/clientlogin", String.Format("accountType=HOSTED_OR_GOOGLE&Email={0}&Passwd={1}&service=lh2&souce=sourceappname", userName, password), out isUserValid);
if (response.Contains("Auth="))
{
foreach (string line in response.Split(Environment.NewLine.ToCharArray()))
{
if (line.StartsWith("Auth="))
{
response = line.Replace("Auth=", "");
break;
}
}
}
else
{
response = "Error:" + response;
}
return response;
}
public static XmlDocument GetRoot(string feedurl, string auth)
{
XmlDocument doc = new XmlDocument();
string result = "";
HttpWebResponse response;
WebRequest request = HttpWebRequest.Create(feedurl);
if (!String.IsNullOrEmpty(auth))
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + auth);
request.Method = "GET";
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
doc.LoadXml(result);
}
else
{
throw new Exception("Fault: Status code - " + response.StatusCode.ToString());
}
return doc;
}
}
public class GoogleRequestHelper
{
public static string GetPostResult(string url, string strPost, out bool isUserValid)
{
string strResponse = "";
try
{
UTF8Encoding objUTFEncode = new UTF8Encoding();
byte[] arrRequest;
Stream objStreamReq;
StreamReader objStreamRes;
HttpWebRequest objHttpRequest;
HttpWebResponse objHttpResponse;
Uri objUri = new Uri(url);
objHttpRequest = (HttpWebRequest)HttpWebRequest.Create(objUri);
objHttpRequest.KeepAlive = false;
objHttpRequest.Method = "POST";
objHttpRequest.ContentType = "application/x-www-form-urlencoded";
arrRequest = objUTFEncode.GetBytes(strPost);
objHttpRequest.ContentLength = arrRequest.Length;
objStreamReq = objHttpRequest.GetRequestStream();
objStreamReq.Write(arrRequest, 0, arrRequest.Length);
objStreamReq.Close();
//Get response
objHttpResponse = (HttpWebResponse)objHttpRequest.GetResponse();
objStreamRes = new StreamReader(objHttpResponse.GetResponseStream(), Encoding.ASCII);
strResponse = objStreamRes.ReadToEnd();
objStreamRes.Close();
isUserValid = true;
}
catch (Exception ex)
{
strResponse = "Error:" + ex.Message;
isUserValid = false;
}
return strResponse;
}
}
{
public static string GetAuthenticationToken(string userName, string password, out bool isUserValid)
{
string response = GoogleRequestHelper.GetPostResult("https://www.google.com/accounts/clientlogin", String.Format("accountType=HOSTED_OR_GOOGLE&Email={0}&Passwd={1}&service=lh2&souce=sourceappname", userName, password), out isUserValid);
if (response.Contains("Auth="))
{
foreach (string line in response.Split(Environment.NewLine.ToCharArray()))
{
if (line.StartsWith("Auth="))
{
response = line.Replace("Auth=", "");
break;
}
}
}
else
{
response = "Error:" + response;
}
return response;
}
public static XmlDocument GetRoot(string feedurl, string auth)
{
XmlDocument doc = new XmlDocument();
string result = "";
HttpWebResponse response;
WebRequest request = HttpWebRequest.Create(feedurl);
if (!String.IsNullOrEmpty(auth))
request.Headers.Add(HttpRequestHeader.Authorization, "GoogleLogin auth=" + auth);
request.Method = "GET";
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
doc.LoadXml(result);
}
else
{
throw new Exception("Fault: Status code - " + response.StatusCode.ToString());
}
return doc;
}
}
public class GoogleRequestHelper
{
public static string GetPostResult(string url, string strPost, out bool isUserValid)
{
string strResponse = "";
try
{
UTF8Encoding objUTFEncode = new UTF8Encoding();
byte[] arrRequest;
Stream objStreamReq;
StreamReader objStreamRes;
HttpWebRequest objHttpRequest;
HttpWebResponse objHttpResponse;
Uri objUri = new Uri(url);
objHttpRequest = (HttpWebRequest)HttpWebRequest.Create(objUri);
objHttpRequest.KeepAlive = false;
objHttpRequest.Method = "POST";
objHttpRequest.ContentType = "application/x-www-form-urlencoded";
arrRequest = objUTFEncode.GetBytes(strPost);
objHttpRequest.ContentLength = arrRequest.Length;
objStreamReq = objHttpRequest.GetRequestStream();
objStreamReq.Write(arrRequest, 0, arrRequest.Length);
objStreamReq.Close();
//Get response
objHttpResponse = (HttpWebResponse)objHttpRequest.GetResponse();
objStreamRes = new StreamReader(objHttpResponse.GetResponseStream(), Encoding.ASCII);
strResponse = objStreamRes.ReadToEnd();
objStreamRes.Close();
isUserValid = true;
}
catch (Exception ex)
{
strResponse = "Error:" + ex.Message;
isUserValid = false;
}
return strResponse;
}
}
Comments
Post a Comment