2010-11-18 52 views
1

我有一個顯示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的?我可以更改參數,但我希望不必對應用程序的其餘部分進行架構更改。謝謝!

+0

嗨TruMan,你真的使它的工作?你能把你的代碼發佈到什麼地方嗎? – Burjua 2011-02-16 16:09:44

回答

0

您應該能夠使用在http://blog.stevienova.com/2008/04/19/oauth-getting-started-with-oauth-in-c-net/處找到的指南作爲編寫代碼以抓取OAuth令牌的基礎。您應該使用https://www.google.com/accounts/OAuthGetRequestToken(如圖所示here),而不是http://term.ie/oauth/example/request_token.php,顯然。我認爲你不需要大幅改變你的架構來實現這個目標。此外,您需要授權令牌,然後才能使用它。我認爲通過http://code.google.com/apis/accounts/docs/OAuth_ref.html閱讀應該能讓你獲得大部分所需。

相關問題