我有一個顯示Google Analytics數據的現有應用程序。目前,它存儲了我不喜歡的用戶名和密碼,所以我想將它轉換爲使用OAuth。我已經分離的身份驗證方法獲得令牌,希望所有我必須做的是改變這個方法:如何從AuthSub遷移到Google 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,你真的使它的工作?你能把你的代碼發佈到什麼地方嗎? – Burjua 2011-02-16 16:09:44