【发布时间】:2011-05-12 00:14:41
【问题描述】:
我有一个显示 Google Analytics(分析)数据的现有应用程序。目前,它存储了我不喜欢的用户名和密码,所以我想将其转换为使用 OAuth。我已经隔离了身份验证方法以获取令牌,希望我所要做的就是更改此方法:
public static string getSessionTokenClientLogin(string email, string password)
{
//Google analytics requires certain variables to be POSTed
string postData = "Email=" + email + "&Passwd=" + password;
//defined - should not channge much
postData = postData + "&accountType=HOSTED_OR_GOOGLE" + "&service=analytics" + "&source=testcomp-testapp-1";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream responseBody = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(responseBody, encode);
//returned from Google Analytics API
string response = readStream.ReadToEnd();
//get the data we need
string[] auth = response.Split(new string[] { "Auth=" }, StringSplitOptions.None);
//return it (the authorization token)
return auth[1];
}
有没有一种简单的方法可以将其转换为 OAuth?我可以更改参数,但我希望不必对应用程序的其余部分进行架构更改。谢谢!
【问题讨论】:
-
嗨,TruMan,你真的成功了吗?你能把你的代码贴在某个地方吗?