代码之家  ›  专栏  ›  技术社区  ›  TruMan1

如何从AuthSub迁移到Google OAuth?

  •  1
  • TruMan1  · 技术社区  · 14 年前

    我有一个显示谷歌分析数据的现有应用程序。目前,它存储我不喜欢的用户名和密码,所以我想将其转换为使用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?我可以更改参数,但我希望我不必对应用程序的其余部分进行架构更改。谢谢!

    1 回复  |  直到 14 年前
        1
  •  0
  •   Brian    14 年前

    你应该可以使用在 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 你应该得到你所需要的。