2010-11-18 6 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

答えて

0

http://blog.stevienova.com/2008/04/19/oauth-getting-started-with-oauth-in-c-net/にあるガイドを使用して、OAuthトークンを取得するためのコードを書くことができます。明らかにhttp://term.ie/oauth/example/request_token.phpの代わりにhttps://www.google.com/accounts/OAuthGetRequestToken(示されているようにhere)を使用する必要があります。私はあなたが実質的にあなたのアーキテクチャを変更する必要があるとは思わないこの作業を得る。また、使用する前にトークンを承認する必要があります。私はhttp://code.google.com/apis/accounts/docs/OAuth_ref.htmlを読んで、あなたが必要とするもののほとんどを得られるはずだと思います。

関連する問題